Skip to content

Commit e719248

Browse files
committed
Suppress "chunk is not well balanced" errors from libxml2.
libxml2 2.13 has an entirely different rule than earlier versions about when to emit "chunk is not well balanced" errors. This causes regression test output discrepancies for three test cases that formerly provoked that error (along with others) and now don't. Closer inspection shows that at least in 2.13, this error is pretty useless because it can only be emitted after some other more-relevant error. So let's get rid of the cross-version discrepancy by just suppressing it. In case some older libxml2 version is capable of emitting this error by itself, suppress only when some other error has already been captured. Like 066e8ac and 6082b3d, this will need to be back-patched, but let's check the results in HEAD first. (The patch for xml_2.out, in particular, is blind since I can't test it here.) 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-bap25 Discussion: https://postgr.es/m/trinity-361ba18b-541a-4fe7-bc63-655ae3a7d599-1720259822452@3c-app-gmx-bs01
1 parent ccd3802 commit e719248

File tree

5 files changed

+81
-12
lines changed

5 files changed

+81
-12
lines changed

src/backend/utils/adt/xml.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2114,6 +2114,19 @@ xml_errorHandler(void *data, PgXmlErrorPtr error)
21142114
switch (domain)
21152115
{
21162116
case XML_FROM_PARSER:
2117+
2118+
/*
2119+
* XML_ERR_NOT_WELL_BALANCED is typically reported after some
2120+
* other, more on-point error. Furthermore, libxml2 2.13 reports
2121+
* it under a completely different set of rules than prior
2122+
* versions. To avoid cross-version behavioral differences,
2123+
* suppress it so long as we already logged some error.
2124+
*/
2125+
if (error->code == XML_ERR_NOT_WELL_BALANCED &&
2126+
xmlerrcxt->err_occurred)
2127+
return;
2128+
/* fall through */
2129+
21172130
case XML_FROM_NONE:
21182131
case XML_FROM_MEMORY:
21192132
case XML_FROM_IO:

src/test/regress/expected/xml.out

Lines changed: 26 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/>');
@@ -294,6 +285,19 @@ SELECT xmlparse(content '<nosuchprefix:tag/>');
294285
<nosuchprefix:tag/>
295286
(1 row)
296287

288+
SELECT xmlparse(content '<unclosed>');
289+
ERROR: invalid XML content
290+
DETAIL: line 1: Premature end of data in tag unclosed line 1
291+
<unclosed>
292+
^
293+
SELECT xmlparse(content '<parent><child></parent></child>');
294+
ERROR: invalid XML content
295+
DETAIL: line 1: Opening and ending tag mismatch: child line 1 and parent
296+
<parent><child></parent></child>
297+
^
298+
line 1: Opening and ending tag mismatch: parent line 1 and child
299+
<parent><child></parent></child>
300+
^
297301
SELECT xmlparse(document ' ');
298302
ERROR: invalid XML document
299303
DETAIL: line 1: Start tag expected, '<' not found
@@ -352,6 +356,19 @@ SELECT xmlparse(document '<nosuchprefix:tag/>');
352356
<nosuchprefix:tag/>
353357
(1 row)
354358

359+
SELECT xmlparse(document '<unclosed>');
360+
ERROR: invalid XML document
361+
DETAIL: line 1: Premature end of data in tag unclosed line 1
362+
<unclosed>
363+
^
364+
SELECT xmlparse(document '<parent><child></parent></child>');
365+
ERROR: invalid XML document
366+
DETAIL: line 1: Opening and ending tag mismatch: child line 1 and parent
367+
<parent><child></parent></child>
368+
^
369+
line 1: Opening and ending tag mismatch: parent line 1 and child
370+
<parent><child></parent></child>
371+
^
355372
SELECT xmlpi(name foo);
356373
xmlpi
357374
---------

src/test/regress/expected/xml_1.out

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,12 @@ DETAIL: This functionality requires the server to be built with libxml support.
180180
SELECT xmlparse(content '<nosuchprefix:tag/>');
181181
ERROR: unsupported XML feature
182182
DETAIL: This functionality requires the server to be built with libxml support.
183+
SELECT xmlparse(content '<unclosed>');
184+
ERROR: unsupported XML feature
185+
DETAIL: This functionality requires the server to be built with libxml support.
186+
SELECT xmlparse(content '<parent><child></parent></child>');
187+
ERROR: unsupported XML feature
188+
DETAIL: This functionality requires the server to be built with libxml support.
183189
SELECT xmlparse(document ' ');
184190
ERROR: unsupported XML feature
185191
DETAIL: This functionality requires the server to be built with libxml support.
@@ -207,6 +213,12 @@ DETAIL: This functionality requires the server to be built with libxml support.
207213
SELECT xmlparse(document '<nosuchprefix:tag/>');
208214
ERROR: unsupported XML feature
209215
DETAIL: This functionality requires the server to be built with libxml support.
216+
SELECT xmlparse(document '<unclosed>');
217+
ERROR: unsupported XML feature
218+
DETAIL: This functionality requires the server to be built with libxml support.
219+
SELECT xmlparse(document '<parent><child></parent></child>');
220+
ERROR: unsupported XML feature
221+
DETAIL: This functionality requires the server to be built with libxml support.
210222
SELECT xmlpi(name foo);
211223
ERROR: unsupported XML feature
212224
DETAIL: This functionality requires the server to be built with libxml support.

src/test/regress/expected/xml_2.out

Lines changed: 26 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,13 +273,25 @@ 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
---------------------
282279
<nosuchprefix:tag/>
283280
(1 row)
284281

282+
SELECT xmlparse(content '<unclosed>');
283+
ERROR: invalid XML content
284+
DETAIL: line 1: Premature end of data in tag unclosed line 1
285+
<unclosed>
286+
^
287+
SELECT xmlparse(content '<parent><child></parent></child>');
288+
ERROR: invalid XML content
289+
DETAIL: line 1: Opening and ending tag mismatch: child line 1 and parent
290+
<parent><child></parent></child>
291+
^
292+
line 1: Opening and ending tag mismatch: parent line 1 and child
293+
<parent><child></parent></child>
294+
^
285295
SELECT xmlparse(document ' ');
286296
ERROR: invalid XML document
287297
DETAIL: line 1: Start tag expected, '<' not found
@@ -332,6 +342,19 @@ SELECT xmlparse(document '<nosuchprefix:tag/>');
332342
<nosuchprefix:tag/>
333343
(1 row)
334344

345+
SELECT xmlparse(document '<unclosed>');
346+
ERROR: invalid XML document
347+
DETAIL: line 1: Premature end of data in tag unclosed line 1
348+
<unclosed>
349+
^
350+
SELECT xmlparse(document '<parent><child></parent></child>');
351+
ERROR: invalid XML document
352+
DETAIL: line 1: Opening and ending tag mismatch: child line 1 and parent
353+
<parent><child></parent></child>
354+
^
355+
line 1: Opening and ending tag mismatch: parent line 1 and child
356+
<parent><child></parent></child>
357+
^
335358
SELECT xmlpi(name foo);
336359
xmlpi
337360
---------

src/test/regress/sql/xml.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ SELECT xmlparse(content '<invalidns xmlns=''&lt;''/>');
7777
SELECT xmlparse(content '<relativens xmlns=''relative''/>');
7878
SELECT xmlparse(content '<twoerrors>&idontexist;</unbalanced>');
7979
SELECT xmlparse(content '<nosuchprefix:tag/>');
80+
SELECT xmlparse(content '<unclosed>');
81+
SELECT xmlparse(content '<parent><child></parent></child>');
8082

8183
SELECT xmlparse(document ' ');
8284
SELECT xmlparse(document 'abc');
@@ -87,6 +89,8 @@ SELECT xmlparse(document '<invalidns xmlns=''&lt;''/>');
8789
SELECT xmlparse(document '<relativens xmlns=''relative''/>');
8890
SELECT xmlparse(document '<twoerrors>&idontexist;</unbalanced>');
8991
SELECT xmlparse(document '<nosuchprefix:tag/>');
92+
SELECT xmlparse(document '<unclosed>');
93+
SELECT xmlparse(document '<parent><child></parent></child>');
9094

9195

9296
SELECT xmlpi(name foo);

0 commit comments

Comments
 (0)