r = MultiByteToWideChar(CP_UTF8, 0, VARDATA_ANY(txt), nbytes,
result, nbytes);
- if (!r) /* assume it's NO_UNICODE_TRANSLATION */
+ if (r <= 0) /* assume it's NO_UNICODE_TRANSLATION */
{
/* see notes above about error reporting */
pg_verifymbstr(VARDATA_ANY(txt), nbytes, false);
}
}
+ /* Append trailing null wchar (MultiByteToWideChar won't have) */
Assert(r <= nbytes);
result[r] = 0;
int nbytes;
int r;
+ /* Compute size of output string (this *will* include trailing null) */
nbytes = WideCharToMultiByte(CP_UTF8, 0, str, -1, NULL, 0, NULL, NULL);
- if (nbytes == 0) /* shouldn't happen */
+ if (nbytes <= 0) /* shouldn't happen */
ereport(ERROR,
(errcode(ERRCODE_CHARACTER_NOT_IN_REPERTOIRE),
errmsg("UTF-16 to UTF-8 translation failed: %lu",
r = WideCharToMultiByte(CP_UTF8, 0, str, -1, VARDATA(result), nbytes,
NULL, NULL);
- if (r == 0) /* shouldn't happen */
+ if (r != nbytes) /* shouldn't happen */
ereport(ERROR,
(errcode(ERRCODE_CHARACTER_NOT_IN_REPERTOIRE),
errmsg("UTF-16 to UTF-8 translation failed: %lu",