99 *
1010 *
1111 * IDENTIFICATION
12- *$PostgreSQL: pgsql/src/backend/utils/adt/oracle_compat.c,v 1.74 2007/11/15 21:14:39 momjian Exp $
12+ *$PostgreSQL: pgsql/src/backend/utils/adt/oracle_compat.c,v 1.75 2007/11/24 21:16:55 tgl Exp $
1313 *
1414 *-------------------------------------------------------------------------
1515 */
@@ -187,7 +187,7 @@ win32_utf8_texttowcs(const text *txt)
187187r = MultiByteToWideChar (CP_UTF8 ,0 ,VARDATA_ANY (txt ),nbytes ,
188188result ,nbytes );
189189
190- if (! r ) /* assume it's NO_UNICODE_TRANSLATION */
190+ if (r <= 0 ) /* assume it's NO_UNICODE_TRANSLATION */
191191{
192192/* see notes above about error reporting */
193193pg_verifymbstr (VARDATA_ANY (txt ),nbytes , false);
@@ -198,6 +198,7 @@ win32_utf8_texttowcs(const text *txt)
198198}
199199}
200200
201+ /* Append trailing null wchar (MultiByteToWideChar won't have) */
201202Assert (r <=nbytes );
202203result [r ]= 0 ;
203204
@@ -212,8 +213,9 @@ win32_utf8_wcstotext(const wchar_t *str)
212213int nbytes ;
213214int r ;
214215
216+ /* Compute size of output string (this *will* include trailing null) */
215217nbytes = WideCharToMultiByte (CP_UTF8 ,0 ,str ,-1 ,NULL ,0 ,NULL ,NULL );
216- if (nbytes = =0 )/* shouldn't happen */
218+ if (nbytes < =0 )/* shouldn't happen */
217219ereport (ERROR ,
218220(errcode (ERRCODE_CHARACTER_NOT_IN_REPERTOIRE ),
219221errmsg ("UTF-16 to UTF-8 translation failed: %lu" ,
@@ -223,7 +225,7 @@ win32_utf8_wcstotext(const wchar_t *str)
223225
224226r = WideCharToMultiByte (CP_UTF8 ,0 ,str ,-1 ,VARDATA (result ),nbytes ,
225227NULL ,NULL );
226- if (r == 0 ) /* shouldn't happen */
228+ if (r != nbytes ) /* shouldn't happen */
227229ereport (ERROR ,
228230(errcode (ERRCODE_CHARACTER_NOT_IN_REPERTOIRE ),
229231errmsg ("UTF-16 to UTF-8 translation failed: %lu" ,