]> git.sur5r.net Git - openldap/blobdiff - libraries/liblmdb/mdb.c
ITS#7992 Tighter utf8_to_utf16(), fix errcodes
[openldap] / libraries / liblmdb / mdb.c
index 057595848607fa41587e24bf5dd1989d2c97f75c..4e4385c04c547846dd71d779e96bfce435524a59 100644 (file)
@@ -4499,9 +4499,9 @@ mdb_env_setup_locks(MDB_env *env, char *lpath, int mode, int *excl)
 #ifdef O_CLOEXEC       /* Linux: Open file and set FD_CLOEXEC atomically */
 #      define MDB_CLOEXEC              O_CLOEXEC
 #else
-       int fdflags;
 #      define MDB_CLOEXEC              0
 #endif
+       int fdflags;
 #endif
        int rc;
        off_t size, rsize;
@@ -4523,12 +4523,12 @@ mdb_env_setup_locks(MDB_env *env, char *lpath, int mode, int *excl)
                if (rc == MDB_ERRCODE_ROFS && (env->me_flags & MDB_RDONLY)) {
                        return MDB_SUCCESS;
                }
-               goto fail_errno;
+               goto fail;
        }
-#if ! ((MDB_CLOEXEC) || defined(_WIN32))
+#ifndef _WIN32
        /* Lose record locks when exec*() */
-       if ((fdflags = fcntl(env->me_lfd, F_GETFD) | FD_CLOEXEC) >= 0)
-                       fcntl(env->me_lfd, F_SETFD, fdflags);
+       if (!(MDB_CLOEXEC) && (fdflags = fcntl(env->me_lfd, F_GETFD)) != -1)
+                       fcntl(env->me_lfd, F_SETFD, fdflags | FD_CLOEXEC);
 #endif
 
        if (!(env->me_flags & MDB_NOTLS)) {
@@ -10104,25 +10104,31 @@ mdb_mutex_failed(MDB_env *env, mdb_mutexref_t mutex, int rc)
        return rc;
 }
 #endif /* MDB_ROBUST_SUPPORTED */
-/** @} */
 
 #if defined(_WIN32)
-static int utf8_to_utf16(const char *src, int srcsize, wchar_t **dst, int *dstsize)
-{
-       int need;
-       wchar_t *result;
-       need = MultiByteToWideChar(CP_UTF8, 0, src, srcsize, NULL, 0);
-       if (need == 0xFFFD)
-               return EILSEQ;
-       if (need == 0)
-               return EINVAL;
-       result = malloc(sizeof(wchar_t) * need);
-       if (!result)
-               return ENOMEM;
-       MultiByteToWideChar(CP_UTF8, 0, src, srcsize, result, need);
-       if (dstsize)
-               *dstsize = need;
-       *dst = result;
-       return 0;
+static int ESECT
+utf8_to_utf16(const char *src, int srcsize, wchar_t **dst, int *dstsize)
+{
+       int rc, need = 0;
+       wchar_t *result = NULL;
+       for (;;) {                                      /* malloc result, then fill it in */
+               need = MultiByteToWideChar(CP_UTF8, 0, src, srcsize, result, need);
+               if (!need) {
+                       rc = ErrCode();
+                       free(result);
+                       return rc;
+               }
+               if (!result) {
+                       result = malloc(sizeof(wchar_t) * need);
+                       if (!result)
+                               return ENOMEM;
+                       continue;
+               }
+               if (dstsize)
+                       *dstsize = need;
+               *dst = result;
+               return MDB_SUCCESS;
+       }
 }
 #endif /* defined(_WIN32) */
+/** @} */