ITS#7992 Tighter utf8_to_utf16(), fix errcodes

The 0xFFFD check seems due to misleading MultiByteToWideChar() doc.
Bad UTF-8 gives 0xFFFD in the output string, not the return value.
This commit is contained in:
Hallvard Furuseth 2016-09-06 18:12:01 +02:00
parent e674f8241c
commit f2ecddbcf7

View file

@ -10104,25 +10104,31 @@ mdb_mutex_failed(MDB_env *env, mdb_mutexref_t mutex, int rc)
return rc; return rc;
} }
#endif /* MDB_ROBUST_SUPPORTED */ #endif /* MDB_ROBUST_SUPPORTED */
/** @} */
#if defined(_WIN32) #if defined(_WIN32)
static int utf8_to_utf16(const char *src, int srcsize, wchar_t **dst, int *dstsize) static int ESECT
utf8_to_utf16(const char *src, int srcsize, wchar_t **dst, int *dstsize)
{ {
int need; int rc, need = 0;
wchar_t *result; wchar_t *result = NULL;
need = MultiByteToWideChar(CP_UTF8, 0, src, srcsize, NULL, 0); for (;;) { /* malloc result, then fill it in */
if (need == 0xFFFD) need = MultiByteToWideChar(CP_UTF8, 0, src, srcsize, result, need);
return EILSEQ; if (!need) {
if (need == 0) rc = ErrCode();
return EINVAL; free(result);
result = malloc(sizeof(wchar_t) * need); return rc;
if (!result) }
return ENOMEM; if (!result) {
MultiByteToWideChar(CP_UTF8, 0, src, srcsize, result, need); result = malloc(sizeof(wchar_t) * need);
if (dstsize) if (!result)
*dstsize = need; return ENOMEM;
*dst = result; continue;
return 0; }
if (dstsize)
*dstsize = need;
*dst = result;
return MDB_SUCCESS;
}
} }
#endif /* defined(_WIN32) */ #endif /* defined(_WIN32) */
/** @} */