Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4813258

Browse files
committed
Make our back branches compatible with libxml2 2.13.x.
This back-patches HEAD commits066e8ac,6082b3d,e719248,and896cd26 into supported branches. Changes:* Use xmlAddChildList not xmlAddChild in XMLSERIALIZE(affects v16 and up only). This was a flat-out coding mistakethat we got away with due to lax checking in previous versionsof xmlAddChild.* Use xmlParseInNodeContext not xmlParseBalancedChunkMemory.This is to dodge a bug in xmlParseBalancedChunkMemory in libxm2releases 2.13.0-2.13.2. While that bug is now fixed upstream andwill probably never be seen in any production-oriented distro, it iscurrently a problem on some more-bleeding-edge-friendly platforms.* Suppress "chunk is not well balanced" errors from libxml2,unless it is the only error. This eliminates an error-reportingdiscrepancy between 2.13 and older releases. This error isalmost always redundant with previous errors, if not flat-outinappropriate, which is why 2.13 changed the behavior and whynobody's likely to miss it.Erik Wienhold and Tom Lane, per report from Frank Streitzig.Discussion:https://postgr.es/m/trinity-b0161630-d230-4598-9ebc-7a23acdb37cb-1720186432160@3c-app-gmx-bap25Discussion:https://postgr.es/m/trinity-361ba18b-541a-4fe7-bc63-655ae3a7d599-1720259822452@3c-app-gmx-bs01
1 parentc774d31 commit4813258

File tree

3 files changed

+58
-31
lines changed

3 files changed

+58
-31
lines changed

‎src/backend/utils/adt/xml.c

Lines changed: 58 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1545,18 +1545,14 @@ xml_parse(text *data, XmlOptionType xmloption_arg, bool preserve_whitespace,
15451545
PG_TRY();
15461546
{
15471547
boolparse_as_document= false;
1548+
intoptions;
15481549
intres_code;
15491550
size_tcount=0;
15501551
xmlChar*version=NULL;
15511552
intstandalone=0;
15521553

15531554
xmlInitParser();
15541555

1555-
ctxt=xmlNewParserCtxt();
1556-
if (ctxt==NULL||xmlerrcxt->err_occurred)
1557-
xml_ereport(xmlerrcxt,ERROR,ERRCODE_OUT_OF_MEMORY,
1558-
"could not allocate parser context");
1559-
15601556
/* Decide whether to parse as document or content */
15611557
if (xmloption_arg==XMLOPTION_DOCUMENT)
15621558
parse_as_document= true;
@@ -1575,20 +1571,30 @@ xml_parse(text *data, XmlOptionType xmloption_arg, bool preserve_whitespace,
15751571
parse_as_document= true;
15761572
}
15771573

1574+
/*
1575+
* Select parse options.
1576+
*
1577+
* Note that here we try to apply DTD defaults (XML_PARSE_DTDATTR)
1578+
* according to SQL/XML:2008 GR 10.16.7.d: 'Default values defined by
1579+
* internal DTD are applied'. As for external DTDs, we try to support
1580+
* them too (see SQL/XML:2008 GR 10.16.7.e), but that doesn't really
1581+
* happen because xmlPgEntityLoader prevents it.
1582+
*/
1583+
options=XML_PARSE_NOENT |XML_PARSE_DTDATTR
1584+
| (preserve_whitespace ?0 :XML_PARSE_NOBLANKS);
1585+
15781586
if (parse_as_document)
15791587
{
1580-
/*
1581-
* Note, that here we try to apply DTD defaults
1582-
* (XML_PARSE_DTDATTR) according to SQL/XML:2008 GR 10.16.7.d:
1583-
* 'Default values defined by internal DTD are applied'. As for
1584-
* external DTDs, we try to support them too, (see SQL/XML:2008 GR
1585-
* 10.16.7.e)
1586-
*/
1588+
ctxt=xmlNewParserCtxt();
1589+
if (ctxt==NULL||xmlerrcxt->err_occurred)
1590+
xml_ereport(xmlerrcxt,ERROR,ERRCODE_OUT_OF_MEMORY,
1591+
"could not allocate parser context");
1592+
15871593
doc=xmlCtxtReadDoc(ctxt,utf8string,
1588-
NULL,
1594+
NULL,/* no URL */
15891595
"UTF-8",
1590-
XML_PARSE_NOENT |XML_PARSE_DTDATTR
1591-
| (preserve_whitespace ?0 :XML_PARSE_NOBLANKS));
1596+
options);
1597+
15921598
if (doc==NULL||xmlerrcxt->err_occurred)
15931599
{
15941600
/* Use original option to decide which error code to throw */
@@ -1602,17 +1608,36 @@ xml_parse(text *data, XmlOptionType xmloption_arg, bool preserve_whitespace,
16021608
}
16031609
else
16041610
{
1611+
xmlNodePtrroot;
1612+
1613+
/* set up document with empty root node to be the context node */
16051614
doc=xmlNewDoc(version);
16061615
Assert(doc->encoding==NULL);
16071616
doc->encoding=xmlStrdup((constxmlChar*)"UTF-8");
16081617
doc->standalone=standalone;
16091618

1619+
root=xmlNewNode(NULL, (constxmlChar*)"content-root");
1620+
if (root==NULL||xmlerrcxt->err_occurred)
1621+
xml_ereport(xmlerrcxt,ERROR,ERRCODE_OUT_OF_MEMORY,
1622+
"could not allocate xml node");
1623+
/* This attaches root to doc, so we need not free it separately. */
1624+
xmlDocSetRootElement(doc,root);
1625+
16101626
/* allow empty content */
16111627
if (*(utf8string+count))
16121628
{
1613-
res_code=xmlParseBalancedChunkMemory(doc,NULL,NULL,0,
1614-
utf8string+count,NULL);
1615-
if (res_code!=0||xmlerrcxt->err_occurred)
1629+
xmlNodePtrnode_list=NULL;
1630+
xmlParserErrorsres;
1631+
1632+
res=xmlParseInNodeContext(root,
1633+
(char*)utf8string+count,
1634+
strlen((char*)utf8string+count),
1635+
options,
1636+
&node_list);
1637+
1638+
xmlFreeNodeList(node_list);
1639+
1640+
if (res!=XML_ERR_OK||xmlerrcxt->err_occurred)
16161641
xml_ereport(xmlerrcxt,ERROR,ERRCODE_INVALID_XML_CONTENT,
16171642
"invalid XML content");
16181643
}
@@ -1631,7 +1656,8 @@ xml_parse(text *data, XmlOptionType xmloption_arg, bool preserve_whitespace,
16311656
}
16321657
PG_END_TRY();
16331658

1634-
xmlFreeParserCtxt(ctxt);
1659+
if (ctxt!=NULL)
1660+
xmlFreeParserCtxt(ctxt);
16351661

16361662
pg_xml_done(xmlerrcxt, false);
16371663

@@ -1812,6 +1838,19 @@ xml_errorHandler(void *data, PgXmlErrorPtr error)
18121838
switch (domain)
18131839
{
18141840
caseXML_FROM_PARSER:
1841+
1842+
/*
1843+
* XML_ERR_NOT_WELL_BALANCED is typically reported after some
1844+
* other, more on-point error. Furthermore, libxml2 2.13 reports
1845+
* it under a completely different set of rules than prior
1846+
* versions. To avoid cross-version behavioral differences,
1847+
* suppress it so long as we already logged some error.
1848+
*/
1849+
if (error->code==XML_ERR_NOT_WELL_BALANCED&&
1850+
xmlerrcxt->err_occurred)
1851+
return;
1852+
/* fall through */
1853+
18151854
caseXML_FROM_NONE:
18161855
caseXML_FROM_MEMORY:
18171856
caseXML_FROM_IO:

‎src/test/regress/expected/xml.out

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -223,17 +223,11 @@ ERROR: invalid XML content
223223
DETAIL: line 1: xmlParseEntityRef: no name
224224
<invalidentity>&</invalidentity>
225225
^
226-
line 1: chunk is not well balanced
227-
<invalidentity>&</invalidentity>
228-
^
229226
SELECT xmlparse(content '<undefinedentity>&idontexist;</undefinedentity>');
230227
ERROR: invalid XML content
231228
DETAIL: line 1: Entity 'idontexist' not defined
232229
<undefinedentity>&idontexist;</undefinedentity>
233230
^
234-
line 1: chunk is not well balanced
235-
<undefinedentity>&idontexist;</undefinedentity>
236-
^
237231
SELECT xmlparse(content '<invalidns xmlns=''&lt;''/>');
238232
xmlparse
239233
---------------------------
@@ -252,9 +246,6 @@ DETAIL: line 1: Entity 'idontexist' not defined
252246
<twoerrors>&idontexist;</unbalanced>
253247
^
254248
line 1: Opening and ending tag mismatch: twoerrors line 1 and unbalanced
255-
<twoerrors>&idontexist;</unbalanced>
256-
^
257-
line 1: chunk is not well balanced
258249
<twoerrors>&idontexist;</unbalanced>
259250
^
260251
SELECT xmlparse(content '<nosuchprefix:tag/>');

‎src/test/regress/expected/xml_2.out

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,13 +219,11 @@ ERROR: invalid XML content
219219
DETAIL: line 1: xmlParseEntityRef: no name
220220
<invalidentity>&</invalidentity>
221221
^
222-
line 1: chunk is not well balanced
223222
SELECT xmlparse(content '<undefinedentity>&idontexist;</undefinedentity>');
224223
ERROR: invalid XML content
225224
DETAIL: line 1: Entity 'idontexist' not defined
226225
<undefinedentity>&idontexist;</undefinedentity>
227226
^
228-
line 1: chunk is not well balanced
229227
SELECT xmlparse(content '<invalidns xmlns=''&lt;''/>');
230228
xmlparse
231229
---------------------------
@@ -244,7 +242,6 @@ DETAIL: line 1: Entity 'idontexist' not defined
244242
<twoerrors>&idontexist;</unbalanced>
245243
^
246244
line 1: Opening and ending tag mismatch: twoerrors line 1 and unbalanced
247-
line 1: chunk is not well balanced
248245
SELECT xmlparse(content '<nosuchprefix:tag/>');
249246
xmlparse
250247
---------------------

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp