Skip to content

Commit 43c7b34

Browse files
committed
check forbidden upper utf8 chars
1 parent 18ba20e commit 43c7b34

File tree

1 file changed

+29
-4
lines changed
  • src/backend/utils/adt

1 file changed

+29
-4
lines changed

src/backend/utils/adt/xml.c

+29-4
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,37 @@ static void SPI_sql_row_to_xmlelement(int rownum, StringInfo result,
150150
static inline void
151151
check_forbidden_chars(char * str)
152152
{
153-
if (strpbrk(str,FORBIDDEN_C0) != NULL)
153+
char * errchar;
154+
155+
if ((errchar = strpbrk(str,FORBIDDEN_C0)) != NULL)
154156
ereport(ERROR,
155157
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
156-
errmsg("character out of range"),
157-
errdetail("XML does not support control characters.")));
158-
158+
errmsg("illegal XML character \\u%.4x", *errchar)));
159+
160+
if (GetDatabaseEncoding() == PG_UTF8)
161+
{
162+
int utf8c;
163+
int utf8len = 0;
164+
while (*str)
165+
{
166+
if (IS_HIGHBIT_SET(*str))
167+
{
168+
utf8c = xmlGetUTF8Char(str, &utf8len);
169+
str += utf8len;
170+
if (! (utf8c < 0xdf00 || (utf8c > 0xdfff &&
171+
utf8c != 0xfffe &&
172+
utf8c != 0xfffe)))
173+
ereport(ERROR,
174+
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
175+
errmsg("illegal XML character \\u%.4x", utf8c)));
176+
}
177+
else
178+
{
179+
/* already checked the forbidden ASCII chars */
180+
str++;
181+
}
182+
}
183+
}
159184
}
160185

161186
#ifdef USE_LIBXML

0 commit comments

Comments
 (0)