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

Commitf85c91a

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 parent0d483ad commitf85c91a

File tree

3 files changed

+66
-36
lines changed

3 files changed

+66
-36
lines changed

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

Lines changed: 66 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,7 @@ xmltotext_with_options(xmltype *data, XmlOptionType xmloption_arg, bool indent)
736736

737737
/* This attaches root to doc, so we need not free it separately. */
738738
xmlDocSetRootElement(doc,root);
739-
xmlAddChild(root,content_nodes);
739+
xmlAddChildList(root,content_nodes);
740740

741741
/*
742742
* We use this node to insert newlines in the dump. Note: in at
@@ -1675,9 +1675,9 @@ xml_doctype_in_content(const xmlChar *str)
16751675
* XmlOptionType actually used to parse the input (typically the same as
16761676
* xmloption_arg, but a DOCTYPE node in the input can force DOCUMENT mode).
16771677
*
1678-
* If parsed_nodes isn't NULL andthe input is not an XML document, the list
1679-
* of parsed nodes from thexmlParseBalancedChunkMemory call will be returned
1680-
* to *parsed_nodes.
1678+
* If parsed_nodes isn't NULL andwe parse in CONTENT mode, the list
1679+
* of parsed nodes from thexmlParseInNodeContext call will be returned
1680+
* to *parsed_nodes. (It is caller's responsibility to free that.)
16811681
*
16821682
* Errors normally result in ereport(ERROR), but if escontext is an
16831683
* ErrorSaveContext, then "safe" errors are reported there instead, and the
@@ -1729,6 +1729,7 @@ xml_parse(text *data, XmlOptionType xmloption_arg,
17291729
PG_TRY();
17301730
{
17311731
boolparse_as_document= false;
1732+
intoptions;
17321733
intres_code;
17331734
size_tcount=0;
17341735
xmlChar*version=NULL;
@@ -1737,11 +1738,6 @@ xml_parse(text *data, XmlOptionType xmloption_arg,
17371738
/* Any errors here are reported as hard ereport's */
17381739
xmlInitParser();
17391740

1740-
ctxt=xmlNewParserCtxt();
1741-
if (ctxt==NULL||xmlerrcxt->err_occurred)
1742-
xml_ereport(xmlerrcxt,ERROR,ERRCODE_OUT_OF_MEMORY,
1743-
"could not allocate parser context");
1744-
17451741
/* Decide whether to parse as document or content */
17461742
if (xmloption_arg==XMLOPTION_DOCUMENT)
17471743
parse_as_document= true;
@@ -1764,6 +1760,18 @@ xml_parse(text *data, XmlOptionType xmloption_arg,
17641760
parse_as_document= true;
17651761
}
17661762

1763+
/*
1764+
* Select parse options.
1765+
*
1766+
* Note that here we try to apply DTD defaults (XML_PARSE_DTDATTR)
1767+
* according to SQL/XML:2008 GR 10.16.7.d: 'Default values defined by
1768+
* internal DTD are applied'. As for external DTDs, we try to support
1769+
* them too (see SQL/XML:2008 GR 10.16.7.e), but that doesn't really
1770+
* happen because xmlPgEntityLoader prevents it.
1771+
*/
1772+
options=XML_PARSE_NOENT |XML_PARSE_DTDATTR
1773+
| (preserve_whitespace ?0 :XML_PARSE_NOBLANKS);
1774+
17671775
/* initialize output parameters */
17681776
if (parsed_xmloptiontype!=NULL)
17691777
*parsed_xmloptiontype=parse_as_document ?XMLOPTION_DOCUMENT :
@@ -1773,18 +1781,16 @@ xml_parse(text *data, XmlOptionType xmloption_arg,
17731781

17741782
if (parse_as_document)
17751783
{
1776-
/*
1777-
* Note, that here we try to apply DTD defaults
1778-
* (XML_PARSE_DTDATTR) according to SQL/XML:2008 GR 10.16.7.d:
1779-
* 'Default values defined by internal DTD are applied'. As for
1780-
* external DTDs, we try to support them too, (see SQL/XML:2008 GR
1781-
* 10.16.7.e)
1782-
*/
1784+
ctxt=xmlNewParserCtxt();
1785+
if (ctxt==NULL||xmlerrcxt->err_occurred)
1786+
xml_ereport(xmlerrcxt,ERROR,ERRCODE_OUT_OF_MEMORY,
1787+
"could not allocate parser context");
1788+
17831789
doc=xmlCtxtReadDoc(ctxt,utf8string,
1784-
NULL,
1790+
NULL,/* no URL */
17851791
"UTF-8",
1786-
XML_PARSE_NOENT |XML_PARSE_DTDATTR
1787-
| (preserve_whitespace ?0 :XML_PARSE_NOBLANKS));
1792+
options);
1793+
17881794
if (doc==NULL||xmlerrcxt->err_occurred)
17891795
{
17901796
/* Use original option to decide which error code to report */
@@ -1801,6 +1807,9 @@ xml_parse(text *data, XmlOptionType xmloption_arg,
18011807
}
18021808
else
18031809
{
1810+
xmlNodePtrroot;
1811+
1812+
/* set up document with empty root node to be the context node */
18041813
doc=xmlNewDoc(version);
18051814
if (doc==NULL||xmlerrcxt->err_occurred)
18061815
xml_ereport(xmlerrcxt,ERROR,ERRCODE_OUT_OF_MEMORY,
@@ -1813,19 +1822,38 @@ xml_parse(text *data, XmlOptionType xmloption_arg,
18131822
"could not allocate XML document");
18141823
doc->standalone=standalone;
18151824

1825+
root=xmlNewNode(NULL, (constxmlChar*)"content-root");
1826+
if (root==NULL||xmlerrcxt->err_occurred)
1827+
xml_ereport(xmlerrcxt,ERROR,ERRCODE_OUT_OF_MEMORY,
1828+
"could not allocate xml node");
1829+
/* This attaches root to doc, so we need not free it separately. */
1830+
xmlDocSetRootElement(doc,root);
1831+
18161832
/* allow empty content */
18171833
if (*(utf8string+count))
18181834
{
1819-
res_code=xmlParseBalancedChunkMemory(doc,NULL,NULL,0,
1820-
utf8string+count,
1821-
parsed_nodes);
1822-
if (res_code!=0||xmlerrcxt->err_occurred)
1835+
xmlNodePtrnode_list=NULL;
1836+
xmlParserErrorsres;
1837+
1838+
res=xmlParseInNodeContext(root,
1839+
(char*)utf8string+count,
1840+
strlen((char*)utf8string+count),
1841+
options,
1842+
&node_list);
1843+
1844+
if (res!=XML_ERR_OK||xmlerrcxt->err_occurred)
18231845
{
1846+
xmlFreeNodeList(node_list);
18241847
xml_errsave(escontext,xmlerrcxt,
18251848
ERRCODE_INVALID_XML_CONTENT,
18261849
"invalid XML content");
18271850
gotofail;
18281851
}
1852+
1853+
if (parsed_nodes!=NULL)
1854+
*parsed_nodes=node_list;
1855+
else
1856+
xmlFreeNodeList(node_list);
18291857
}
18301858
}
18311859

@@ -1845,7 +1873,8 @@ xml_parse(text *data, XmlOptionType xmloption_arg,
18451873
}
18461874
PG_END_TRY();
18471875

1848-
xmlFreeParserCtxt(ctxt);
1876+
if (ctxt!=NULL)
1877+
xmlFreeParserCtxt(ctxt);
18491878

18501879
pg_xml_done(xmlerrcxt, false);
18511880

@@ -2064,6 +2093,19 @@ xml_errorHandler(void *data, PgXmlErrorPtr error)
20642093
switch (domain)
20652094
{
20662095
caseXML_FROM_PARSER:
2096+
2097+
/*
2098+
* XML_ERR_NOT_WELL_BALANCED is typically reported after some
2099+
* other, more on-point error. Furthermore, libxml2 2.13 reports
2100+
* it under a completely different set of rules than prior
2101+
* versions. To avoid cross-version behavioral differences,
2102+
* suppress it so long as we already logged some error.
2103+
*/
2104+
if (error->code==XML_ERR_NOT_WELL_BALANCED&&
2105+
xmlerrcxt->err_occurred)
2106+
return;
2107+
/* fall through */
2108+
20672109
caseXML_FROM_NONE:
20682110
caseXML_FROM_MEMORY:
20692111
caseXML_FROM_IO:

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

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -254,17 +254,11 @@ ERROR: invalid XML content
254254
DETAIL: line 1: xmlParseEntityRef: no name
255255
<invalidentity>&</invalidentity>
256256
^
257-
line 1: chunk is not well balanced
258-
<invalidentity>&</invalidentity>
259-
^
260257
SELECT xmlparse(content '<undefinedentity>&idontexist;</undefinedentity>');
261258
ERROR: invalid XML content
262259
DETAIL: line 1: Entity 'idontexist' not defined
263260
<undefinedentity>&idontexist;</undefinedentity>
264261
^
265-
line 1: chunk is not well balanced
266-
<undefinedentity>&idontexist;</undefinedentity>
267-
^
268262
SELECT xmlparse(content '<invalidns xmlns=''&lt;''/>');
269263
xmlparse
270264
---------------------------
@@ -283,9 +277,6 @@ DETAIL: line 1: Entity 'idontexist' not defined
283277
<twoerrors>&idontexist;</unbalanced>
284278
^
285279
line 1: Opening and ending tag mismatch: twoerrors line 1 and unbalanced
286-
<twoerrors>&idontexist;</unbalanced>
287-
^
288-
line 1: chunk is not well balanced
289280
<twoerrors>&idontexist;</unbalanced>
290281
^
291282
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
@@ -250,13 +250,11 @@ ERROR: invalid XML content
250250
DETAIL: line 1: xmlParseEntityRef: no name
251251
<invalidentity>&</invalidentity>
252252
^
253-
line 1: chunk is not well balanced
254253
SELECT xmlparse(content '<undefinedentity>&idontexist;</undefinedentity>');
255254
ERROR: invalid XML content
256255
DETAIL: line 1: Entity 'idontexist' not defined
257256
<undefinedentity>&idontexist;</undefinedentity>
258257
^
259-
line 1: chunk is not well balanced
260258
SELECT xmlparse(content '<invalidns xmlns=''&lt;''/>');
261259
xmlparse
262260
---------------------------
@@ -275,7 +273,6 @@ DETAIL: line 1: Entity 'idontexist' not defined
275273
<twoerrors>&idontexist;</unbalanced>
276274
^
277275
line 1: Opening and ending tag mismatch: twoerrors line 1 and unbalanced
278-
line 1: chunk is not well balanced
279276
SELECT xmlparse(content '<nosuchprefix:tag/>');
280277
xmlparse
281278
---------------------

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp