diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2008-11-10 18:02:27 +0000 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2008-11-10 18:02:27 +0000 |
commit | 47925b83cb1451ceb4fa37e70ee46eef064bd412 (patch) | |
tree | ed25cf14a6850e59403ada020a2eab0134bec200 /src | |
parent | af9b481653240fe1ffd66f184026ea24928abcb2 (diff) | |
download | postgresql-47925b83cb1451ceb4fa37e70ee46eef064bd412.tar.gz postgresql-47925b83cb1451ceb4fa37e70ee46eef064bd412.zip |
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.
Diffstat (limited to 'src')
-rw-r--r-- | src/backend/utils/adt/xml.c | 26 |
1 files changed, 19 insertions, 7 deletions
diff --git a/src/backend/utils/adt/xml.c b/src/backend/utils/adt/xml.c index 98781cc523b..e3cd95bedd4 100644 --- a/src/backend/utils/adt/xml.c +++ b/src/backend/utils/adt/xml.c @@ -7,7 +7,7 @@ * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $PostgreSQL: pgsql/src/backend/utils/adt/xml.c,v 1.68.2.5 2008/10/09 15:49:10 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/utils/adt/xml.c,v 1.68.2.6 2008/11/10 18:02:27 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -1447,7 +1447,11 @@ sqlchar_to_unicode(char *s) GetDatabaseEncoding(), PG_UTF8); - pg_encoding_mb2wchar_with_len(PG_UTF8, utf8string, ret, pg_mblen(s)); + pg_encoding_mb2wchar_with_len(PG_UTF8, utf8string, ret, + pg_encoding_mblen(PG_UTF8, utf8string)); + + if (utf8string != s) + pfree(utf8string); return ret[0]; } @@ -1537,7 +1541,10 @@ map_sql_identifier_to_xml_name(char *ident, bool fully_escaped, static char * unicode_to_sqlchar(pg_wchar c) { - static unsigned char utf8string[5]; /* need trailing zero */ + unsigned char utf8string[5]; /* need room for trailing zero */ + char *result; + + memset(utf8string, 0, sizeof(utf8string)); if (c <= 0x7F) { @@ -1562,10 +1569,15 @@ unicode_to_sqlchar(pg_wchar c) utf8string[3] = 0x80 | (c & 0x3F); } - return (char *) pg_do_encoding_conversion(utf8string, - pg_mblen((char *) utf8string), - PG_UTF8, - GetDatabaseEncoding()); + result = (char *) pg_do_encoding_conversion(utf8string, + pg_encoding_mblen(PG_UTF8, + (char *) utf8string), + PG_UTF8, + GetDatabaseEncoding()); + /* if pg_do_encoding_conversion didn't strdup, we must */ + if (result == (char *) utf8string) + result = pstrdup(result); + return result; } |