Skip to content

Commit ae3ff7a

Browse files
committed
Fix (I think) broken usage of MultiByteToWideChar. I had missed the
subtlety that this function only returns a null terminator if it's fed input that includes one; which, in the usage here, it's not. This probably fixes bugs reported by Thomas Haegi.
1 parent 0f20e7a commit ae3ff7a

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

src/backend/tsearch/ts_locale.c

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $PostgreSQL: pgsql/src/backend/tsearch/ts_locale.c,v 1.4 2007/11/15 21:14:38 momjian Exp $
10+
* $PostgreSQL: pgsql/src/backend/tsearch/ts_locale.c,v 1.5 2007/11/24 21:20:07 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -23,7 +23,7 @@
2323
* wchar2char --- convert wide characters to multibyte format
2424
*
2525
* This has the same API as the standard wcstombs() function; in particular,
26-
* tolen is the maximum number of bytes to store at *to, and *from should be
26+
* tolen is the maximum number of bytes to store at *to, and *from must be
2727
* zero-terminated. The output will be zero-terminated iff there is room.
2828
*/
2929
size_t
@@ -73,21 +73,28 @@ char2wchar(wchar_t *to, size_t tolen, const char *from, size_t fromlen)
7373
{
7474
int r;
7575

76-
r = MultiByteToWideChar(CP_UTF8, 0, from, fromlen, to, tolen);
77-
78-
if (r <= 0)
76+
/* stupid Microsloth API does not work for zero-length input */
77+
if (fromlen == 0)
78+
r = 0;
79+
else
7980
{
80-
pg_verifymbstr(from, fromlen, false);
81-
ereport(ERROR,
82-
(errcode(ERRCODE_CHARACTER_NOT_IN_REPERTOIRE),
83-
errmsg("invalid multibyte character for locale"),
84-
errhint("The server's LC_CTYPE locale is probably incompatible with the database encoding.")));
81+
r = MultiByteToWideChar(CP_UTF8, 0, from, fromlen, to, tolen - 1);
82+
83+
if (r <= 0)
84+
{
85+
/* see notes in oracle_compat.c about error reporting */
86+
pg_verifymbstr(from, fromlen, false);
87+
ereport(ERROR,
88+
(errcode(ERRCODE_CHARACTER_NOT_IN_REPERTOIRE),
89+
errmsg("invalid multibyte character for locale"),
90+
errhint("The server's LC_CTYPE locale is probably incompatible with the database encoding.")));
91+
}
8592
}
8693

87-
Assert(r <= tolen);
94+
Assert(r < tolen);
95+
to[r] = 0;
8896

89-
/* Microsoft counts the zero terminator in the result */
90-
return r - 1;
97+
return r;
9198
}
9299
#endif /* WIN32 */
93100

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy