Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 57b1085

Browse files
committed
Allow empty content in xml type
The xml type previously rejected "content" that is empty or consists only of spaces. But the SQL/XML standard allows that, so change that. The accepted values for XML "documents" are not changed. Reviewed-by: Ali Akbar <the.apaan@gmail.com>
1 parent f0051c1 commit 57b1085

File tree

4 files changed

+57
-5
lines changed

4 files changed

+57
-5
lines changed

src/backend/utils/adt/xml.c

+9-5
Original file line numberDiff line numberDiff line change
@@ -1400,11 +1400,15 @@ xml_parse(text *data, XmlOptionType xmloption_arg, bool preserve_whitespace,
14001400
doc->encoding = xmlStrdup((const xmlChar *) "UTF-8");
14011401
doc->standalone = standalone;
14021402

1403-
res_code = xmlParseBalancedChunkMemory(doc, NULL, NULL, 0,
1404-
utf8string + count, NULL);
1405-
if (res_code != 0 || xmlerrcxt->err_occurred)
1406-
xml_ereport(xmlerrcxt, ERROR, ERRCODE_INVALID_XML_CONTENT,
1407-
"invalid XML content");
1403+
/* allow empty content */
1404+
if (*(utf8string + count))
1405+
{
1406+
res_code = xmlParseBalancedChunkMemory(doc, NULL, NULL, 0,
1407+
utf8string + count, NULL);
1408+
if (res_code != 0 || xmlerrcxt->err_occurred)
1409+
xml_ereport(xmlerrcxt, ERROR, ERRCODE_INVALID_XML_CONTENT,
1410+
"invalid XML content");
1411+
}
14081412
}
14091413
}
14101414
PG_CATCH();

src/test/regress/expected/xml.out

+28
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,18 @@ SELECT xmlelement(name foo, xmlattributes('<>&"''' as funny, xml 'b<a/>r' as fun
194194
<foo funny="&lt;&gt;&amp;&quot;'" funnier="b&lt;a/&gt;r"/>
195195
(1 row)
196196

197+
SELECT xmlparse(content '');
198+
xmlparse
199+
----------
200+
201+
(1 row)
202+
203+
SELECT xmlparse(content ' ');
204+
xmlparse
205+
----------
206+
207+
(1 row)
208+
197209
SELECT xmlparse(content 'abc');
198210
xmlparse
199211
----------
@@ -251,6 +263,22 @@ SELECT xmlparse(content '<nosuchprefix:tag/>');
251263
<nosuchprefix:tag/>
252264
(1 row)
253265

266+
SELECT xmlparse(document '');
267+
ERROR: invalid XML document
268+
DETAIL: line 1: switching encoding : no input
269+
270+
^
271+
line 1: Document is empty
272+
273+
^
274+
line 1: Start tag expected, '<' not found
275+
276+
^
277+
SELECT xmlparse(document ' ');
278+
ERROR: invalid XML document
279+
DETAIL: line 1: Start tag expected, '<' not found
280+
281+
^
254282
SELECT xmlparse(document 'abc');
255283
ERROR: invalid XML document
256284
DETAIL: line 1: Start tag expected, '<' not found

src/test/regress/expected/xml_1.out

+16
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,14 @@ SELECT xmlelement(name foo, xmlattributes('<>&"''' as funny, xml 'b<a/>r' as fun
164164
ERROR: unsupported XML feature
165165
DETAIL: This functionality requires the server to be built with libxml support.
166166
HINT: You need to rebuild PostgreSQL using --with-libxml.
167+
SELECT xmlparse(content '');
168+
ERROR: unsupported XML feature
169+
DETAIL: This functionality requires the server to be built with libxml support.
170+
HINT: You need to rebuild PostgreSQL using --with-libxml.
171+
SELECT xmlparse(content ' ');
172+
ERROR: unsupported XML feature
173+
DETAIL: This functionality requires the server to be built with libxml support.
174+
HINT: You need to rebuild PostgreSQL using --with-libxml.
167175
SELECT xmlparse(content 'abc');
168176
ERROR: unsupported XML feature
169177
DETAIL: This functionality requires the server to be built with libxml support.
@@ -196,6 +204,14 @@ SELECT xmlparse(content '<nosuchprefix:tag/>');
196204
ERROR: unsupported XML feature
197205
DETAIL: This functionality requires the server to be built with libxml support.
198206
HINT: You need to rebuild PostgreSQL using --with-libxml.
207+
SELECT xmlparse(document '');
208+
ERROR: unsupported XML feature
209+
DETAIL: This functionality requires the server to be built with libxml support.
210+
HINT: You need to rebuild PostgreSQL using --with-libxml.
211+
SELECT xmlparse(document ' ');
212+
ERROR: unsupported XML feature
213+
DETAIL: This functionality requires the server to be built with libxml support.
214+
HINT: You need to rebuild PostgreSQL using --with-libxml.
199215
SELECT xmlparse(document 'abc');
200216
ERROR: unsupported XML feature
201217
DETAIL: This functionality requires the server to be built with libxml support.

src/test/regress/sql/xml.sql

+4
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ SELECT xmlelement(name foo, xmlattributes('infinity'::timestamp as bar));
6060
SELECT xmlelement(name foo, xmlattributes('<>&"''' as funny, xml 'b<a/>r' as funnier));
6161

6262

63+
SELECT xmlparse(content '');
64+
SELECT xmlparse(content ' ');
6365
SELECT xmlparse(content 'abc');
6466
SELECT xmlparse(content '<abc>x</abc>');
6567
SELECT xmlparse(content '<invalidentity>&</invalidentity>');
@@ -69,6 +71,8 @@ SELECT xmlparse(content '<relativens xmlns=''relative''/>');
6971
SELECT xmlparse(content '<twoerrors>&idontexist;</unbalanced>');
7072
SELECT xmlparse(content '<nosuchprefix:tag/>');
7173

74+
SELECT xmlparse(document '');
75+
SELECT xmlparse(document ' ');
7276
SELECT xmlparse(document 'abc');
7377
SELECT xmlparse(document '<abc>x</abc>');
7478
SELECT xmlparse(document '<invalidentity>&</abc>');

0 commit comments

Comments
 (0)