Skip to content

Commit 5e11e73

Browse files
committed
Fix bugs in sqlchar_to_unicode and unicode_to_sqlchar: both were measuring
the length of a UTF8 character with pg_mblen (wrong if DB encoding isn't UTF8), and the latter was blithely assuming that a static buffer would somehow revert to all zeroes for each use.
1 parent 45d146a commit 5e11e73

File tree

1 file changed

+18
-7
lines changed
  • src/backend/utils/adt

1 file changed

+18
-7
lines changed

src/backend/utils/adt/xml.c

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/backend/utils/adt/xml.c,v 1.80 2008/10/29 08:04:53 petere Exp $
10+
* $PostgreSQL: pgsql/src/backend/utils/adt/xml.c,v 1.81 2008/11/10 18:02:20 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -1405,7 +1405,11 @@ sqlchar_to_unicode(char *s)
14051405
GetDatabaseEncoding(),
14061406
PG_UTF8);
14071407

1408-
pg_encoding_mb2wchar_with_len(PG_UTF8, utf8string, ret, pg_mblen(s));
1408+
pg_encoding_mb2wchar_with_len(PG_UTF8, utf8string, ret,
1409+
pg_encoding_mblen(PG_UTF8, utf8string));
1410+
1411+
if (utf8string != s)
1412+
pfree(utf8string);
14091413

14101414
return ret[0];
14111415
}
@@ -1495,14 +1499,21 @@ map_sql_identifier_to_xml_name(char *ident, bool fully_escaped,
14951499
static char *
14961500
unicode_to_sqlchar(pg_wchar c)
14971501
{
1498-
static unsigned char utf8string[5]; /* need trailing zero */
1502+
unsigned char utf8string[5]; /* need room for trailing zero */
1503+
char *result;
14991504

1505+
memset(utf8string, 0, sizeof(utf8string));
15001506
unicode_to_utf8(c, utf8string);
15011507

1502-
return (char *) pg_do_encoding_conversion(utf8string,
1503-
pg_mblen((char *) utf8string),
1504-
PG_UTF8,
1505-
GetDatabaseEncoding());
1508+
result = (char *) pg_do_encoding_conversion(utf8string,
1509+
pg_encoding_mblen(PG_UTF8,
1510+
(char *) utf8string),
1511+
PG_UTF8,
1512+
GetDatabaseEncoding());
1513+
/* if pg_do_encoding_conversion didn't strdup, we must */
1514+
if (result == (char *) utf8string)
1515+
result = pstrdup(result);
1516+
return result;
15061517
}
15071518

15081519

0 commit comments

Comments
 (0)