Skip to content

Commit c554672

Browse files
nielsdosremicollet
authored andcommitted
Backport 4fe82131: Backport libxml2 2.13.2 fixes (#14816)
Backproted from php/php-src#14789 (cherry picked from commit bb46b4b799b583528025a775af45308133bfd4c1)
1 parent dfed528 commit c554672

18 files changed

+97
-29
lines changed

ext/dom/document.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1253,11 +1253,13 @@ static xmlDocPtr dom_document_parser(zval *id, int mode, char *source, size_t so
12531253
if (keep_blanks == 0 && ! (options & XML_PARSE_NOBLANKS)) {
12541254
options |= XML_PARSE_NOBLANKS;
12551255
}
1256+
if (recover) {
1257+
options |= XML_PARSE_RECOVER;
1258+
}
12561259

12571260
php_libxml_sanitize_parse_ctxt_options(ctxt);
12581261
xmlCtxtUseOptions(ctxt, options);
12591262

1260-
ctxt->recovery = recover;
12611263
if (recover) {
12621264
old_error_reporting = EG(error_reporting);
12631265
EG(error_reporting) = old_error_reporting | E_WARNING;
@@ -1267,7 +1269,7 @@ static xmlDocPtr dom_document_parser(zval *id, int mode, char *source, size_t so
12671269

12681270
if (ctxt->wellFormed || recover) {
12691271
ret = ctxt->myDoc;
1270-
if (ctxt->recovery) {
1272+
if (recover) {
12711273
EG(error_reporting) = old_error_reporting;
12721274
}
12731275
/* If loading from memory, set the base reference uri for the document */

ext/dom/tests/DOMDocument_loadHTMLfile_error1.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ $result = $doc->loadHTMLFile(__DIR__ . "/ffff/test.html");
1515
assert($result === false);
1616
?>
1717
--EXPECTF--
18-
%r(PHP ){0,1}%rWarning: DOMDocument::loadHTMLFile(): I/O warning : failed to load external entity %s
18+
%r(PHP ){0,1}%rWarning: DOMDocument::loadHTMLFile(): I/O %s

ext/dom/tests/DOMDocument_loadXML_error2_pre2_11.phpt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Test DOMDocument::loadXML() detects not-well formed XML
33
--SKIPIF--
44
<?php
5+
include('skipif.inc');
56
if (LIBXML_VERSION >= 21100) die('skip libxml2 test variant for version < 2.11');
67
?>
78
--DESCRIPTION--
@@ -14,8 +15,6 @@ Environment variables used in the test:
1415
Antonio Diaz Ruiz <[email protected]>
1516
--INI--
1617
assert.bail=true
17-
--SKIPIF--
18-
<?php include('skipif.inc'); ?>
1918
--ENV--
2019
XML_FILE=/not_well_formed2.xml
2120
LOAD_OPTIONS=0

ext/dom/tests/DOMDocument_load_error2_pre2_11.phpt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Test DOMDocument::load() detects not-well formed XML
33
--SKIPIF--
44
<?php
5+
include('skipif.inc');
56
if (LIBXML_VERSION >= 21100) die('skip libxml2 test variant for version < 2.11');
67
?>
78
--DESCRIPTION--
@@ -14,8 +15,6 @@ Environment variables used in the test:
1415
Antonio Diaz Ruiz <[email protected]>
1516
--INI--
1617
assert.bail=true
17-
--SKIPIF--
18-
<?php include('skipif.inc'); ?>
1918
--ENV--
2019
XML_FILE=/not_well_formed2.xml
2120
LOAD_OPTIONS=0

ext/dom/tests/DOMDocument_relaxNGValidate_error2.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ $result = $doc->relaxNGValidate($rng);
2222
var_dump($result);
2323
?>
2424
--EXPECTF--
25-
Warning: DOMDocument::relaxNGValidate(): I/O warning : failed to load external entity "%s/foo.rng" in %s on line %d
25+
Warning: DOMDocument::relaxNGValidate(): I/O %s : failed to load %s
2626

2727
Warning: DOMDocument::relaxNGValidate(): xmlRelaxNGParse: could not load %s/foo.rng in %s on line %d
2828

ext/dom/tests/DOMDocument_saveHTMLFile_basic.phpt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Knut Urdalen <[email protected]>
66
--SKIPIF--
77
<?php
88
require_once __DIR__ .'/skipif.inc';
9+
if (LIBXML_VERSION >= 21300) die("skip see https://gitlab.gnome.org/GNOME/libxml2/-/issues/756");
910
?>
1011
--FILE--
1112
<?php

ext/dom/tests/DOMDocument_saveHTMLFile_formatOutput.phpt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Knut Urdalen <[email protected]>
66
--SKIPIF--
77
<?php
88
require_once __DIR__ .'/skipif.inc';
9+
if (LIBXML_VERSION >= 21300) die("skip see https://gitlab.gnome.org/GNOME/libxml2/-/issues/756");
910
?>
1011
--FILE--
1112
<?php
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
--TEST--
2+
DOMDocument::saveHTMLFile() should format output on demand
3+
--CREDITS--
4+
Knut Urdalen <[email protected]>
5+
#PHPTestFest2009 Norway 2009-06-09 \o/
6+
--EXTENSIONS--
7+
dom
8+
--SKIPIF--
9+
<?php
10+
if (LIBXML_VERSION < 21300) die("skip see https://gitlab.gnome.org/GNOME/libxml2/-/issues/756");
11+
?>
12+
--FILE--
13+
<?php
14+
$filename = __DIR__."/DOMDocument_saveHTMLFile_formatOutput_gte_2_13.html";
15+
$doc = new DOMDocument('1.0');
16+
$doc->formatOutput = true;
17+
$root = $doc->createElement('html');
18+
$root = $doc->appendChild($root);
19+
$head = $doc->createElement('head');
20+
$head = $root->appendChild($head);
21+
$title = $doc->createElement('title');
22+
$title = $head->appendChild($title);
23+
$text = $doc->createTextNode('This is the title');
24+
$text = $title->appendChild($text);
25+
$bytes = $doc->saveHTMLFile($filename);
26+
var_dump($bytes);
27+
echo file_get_contents($filename);
28+
unlink($filename);
29+
?>
30+
--EXPECT--
31+
int(59)
32+
<html><head><title>This is the title</title></head></html>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--TEST--
2+
DOMDocument::saveHTMLFile() should dump the internal document into a file using HTML formatting
3+
--CREDITS--
4+
Knut Urdalen <[email protected]>
5+
#PHPTestFest2009 Norway 2009-06-09 \o/
6+
--EXTENSIONS--
7+
dom
8+
--SKIPIF--
9+
<?php
10+
if (LIBXML_VERSION < 21300) die("skip see https://gitlab.gnome.org/GNOME/libxml2/-/issues/756");
11+
?>
12+
--FILE--
13+
<?php
14+
$filename = __DIR__."/DOMDocument_saveHTMLFile_basic_gte_2_13.html";
15+
$doc = new DOMDocument('1.0');
16+
$root = $doc->createElement('html');
17+
$root = $doc->appendChild($root);
18+
$head = $doc->createElement('head');
19+
$head = $root->appendChild($head);
20+
$title = $doc->createElement('title');
21+
$title = $head->appendChild($title);
22+
$text = $doc->createTextNode('This is the title');
23+
$text = $title->appendChild($text);
24+
$bytes = $doc->saveHTMLFile($filename);
25+
var_dump($bytes);
26+
echo file_get_contents($filename);
27+
unlink($filename);
28+
?>
29+
--EXPECT--
30+
int(59)
31+
<html><head><title>This is the title</title></head></html>

ext/dom/tests/DOMDocument_schemaValidate_error5.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var_dump($result);
1717

1818
?>
1919
--EXPECTF--
20-
Warning: DOMDocument::schemaValidate(): I/O warning : failed to load external entity "%snon-existent-file" in %s.php on line %d
20+
Warning: DOMDocument::schemaValidate(): I/O %s : failed to load %s
2121

2222
Warning: DOMDocument::schemaValidate(): Failed to locate the main schema resource at '%s/non-existent-file'. in %s.php on line %d
2323

ext/dom/tests/dom_create_element.phpt

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -251,14 +251,10 @@ try {
251251
print $e->getMessage() . "\n";
252252
}
253253

254-
/* This isn't because the xml namespace isn't there and we can't create it */
255-
print "29 DOMElement::__construct('xml:valid', '', 'http://www.w3.org/XML/1998/namespace')\n";
256-
try {
257-
$element = new DomElement('xml:valid', '', 'http://www.w3.org/XML/1998/namespace');
258-
print "valid\n";
259-
} catch (Exception $e) {
260-
print $e->getMessage() . "\n";
261-
}
254+
/* There used to be a 29 here that tested DOMElement::__construct('xml:valid', '', 'http://www.w3.org/XML/1998/namespace').
255+
* In libxml2 version 2.12 or prior this didn't work because the xml namespace isn't there and you can't create it without
256+
* a document. Starting from libxml2 version 2.13 it does actually work because the XML namespace is statically defined.
257+
* The behaviour from version 2.13 is actually the desired behaviour anyway. */
262258

263259

264260
/* the qualifiedName or its prefix is "xmlns" and the namespaceURI is
@@ -378,8 +374,6 @@ Namespace Error
378374
Namespace Error
379375
28 DOMDocument::createElementNS('http://www.w3.org/XML/1998/namespace', 'xml:valid')
380376
valid
381-
29 DOMElement::__construct('xml:valid', '', 'http://www.w3.org/XML/1998/namespace')
382-
Namespace Error
383377
30 DOMDocument::createElementNS('http://wrong.namespaceURI.com', 'xmlns:valid')
384378
Namespace Error
385379
31 DOMElement::__construct('xmlns:valid', '', 'http://wrong.namespaceURI.com')

ext/libxml/libxml.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,8 +428,10 @@ php_libxml_input_buffer_create_filename(const char *URI, xmlCharEncoding enc)
428428
static xmlOutputBufferPtr
429429
php_libxml_output_buffer_create_filename(const char *URI,
430430
xmlCharEncodingHandlerPtr encoder,
431-
int compression ATTRIBUTE_UNUSED)
431+
int compression)
432432
{
433+
ZEND_IGNORE_VALUE(compression);
434+
433435
xmlOutputBufferPtr ret;
434436
xmlURIPtr puri;
435437
void *context = NULL;

ext/simplexml/tests/bug79971_1.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ var_dump($sxe->asXML("$uri.out%00foo"));
2020
--EXPECTF--
2121
Warning: simplexml_load_file(): URI must not contain percent-encoded NUL bytes in %s on line %d
2222

23-
Warning: simplexml_load_file(): I/O warning : failed to load external entity "%s/bug79971_1.xml%00foo" in %s on line %d
23+
Warning: simplexml_load_file(): I/O warning : failed to load %s
2424
bool(false)
2525

2626
Warning: SimpleXMLElement::asXML(): URI must not contain percent-encoded NUL bytes in %s on line %d

ext/soap/php_encoding.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3379,7 +3379,6 @@ xmlNsPtr encode_add_ns(xmlNodePtr node, const char* ns)
33793379
} else {
33803380
smart_str prefix = {0};
33813381
int num = ++SOAP_GLOBAL(cur_uniq_ns);
3382-
xmlChar *enc_ns;
33833382

33843383
while (1) {
33853384
smart_str_appendl(&prefix, "ns", 2);
@@ -3393,9 +3392,15 @@ xmlNsPtr encode_add_ns(xmlNodePtr node, const char* ns)
33933392
num = ++SOAP_GLOBAL(cur_uniq_ns);
33943393
}
33953394

3396-
enc_ns = xmlEncodeSpecialChars(node->doc, BAD_CAST(ns));
3395+
/* Starting with libxml 2.13, we don't have to do this workaround anymore, otherwise we get double-encoded
3396+
* entities. See libxml2 commit f506ec66547ef9bac97a2bf306d368ecea8c0c9e. */
3397+
#if LIBXML_VERSION < 21300
3398+
xmlChar *enc_ns = xmlEncodeSpecialChars(node->doc, BAD_CAST(ns));
33973399
xmlns = xmlNewNs(node->doc->children, enc_ns, BAD_CAST(prefix.s ? ZSTR_VAL(prefix.s) : ""));
33983400
xmlFree(enc_ns);
3401+
#else
3402+
xmlns = xmlNewNs(node->doc->children, BAD_CAST(ns), BAD_CAST(prefix.s ? ZSTR_VAL(prefix.s) : ""));
3403+
#endif
33993404
smart_str_free(&prefix);
34003405
}
34013406
}

ext/soap/php_xml.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,14 @@ xmlDocPtr soap_xmlParseFile(const char *filename)
9292
zend_bool old;
9393

9494
php_libxml_sanitize_parse_ctxt_options(ctxt);
95+
/* TODO: In libxml2 2.14.0 change this to the new options API so we don't rely on deprecated APIs. */
9596
ctxt->keepBlanks = 0;
97+
ctxt->options |= XML_PARSE_HUGE;
9698
ctxt->sax->ignorableWhitespace = soap_ignorableWhitespace;
9799
ctxt->sax->comment = soap_Comment;
98100
ctxt->sax->warning = NULL;
99101
ctxt->sax->error = NULL;
100102
/*ctxt->sax->fatalError = NULL;*/
101-
ctxt->options |= XML_PARSE_HUGE;
102103
old = php_libxml_disable_entity_loader(1);
103104
xmlParseDocument(ctxt);
104105
php_libxml_disable_entity_loader(old);
@@ -146,6 +147,7 @@ xmlDocPtr soap_xmlParseMemory(const void *buf, size_t buf_size)
146147
ctxt->sax->warning = NULL;
147148
ctxt->sax->error = NULL;
148149
/*ctxt->sax->fatalError = NULL;*/
150+
/* TODO: In libxml2 2.14.0 change this to the new options API so we don't rely on deprecated APIs. */
149151
ctxt->options |= XML_PARSE_HUGE;
150152
old = php_libxml_disable_entity_loader(1);
151153
xmlParseDocument(ctxt);

ext/soap/tests/bugs/bug42151.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ try {
2525
}
2626
echo "ok\n";
2727
?>
28-
--EXPECT--
29-
SOAP-ERROR: Parsing WSDL: Couldn't load from 'httpx://' : failed to load external entity "httpx://"
28+
--EXPECTF--
29+
SOAP-ERROR: Parsing WSDL: Couldn't load from 'httpx://' : failed to load %s
3030

3131
ok
3232
I don't get executed either.

ext/xml/compat.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -714,8 +714,7 @@ XML_GetCurrentByteCount(XML_Parser parser)
714714
{
715715
/* WARNING: this is identical to ByteIndex; it should probably
716716
* be different */
717-
return parser->parser->input->consumed +
718-
(parser->parser->input->cur - parser->parser->input->base);
717+
return XML_GetCurrentByteIndex(parser);
719718
}
720719

721720
PHP_XML_API const XML_Char *XML_ExpatVersion(void)

ext/xmlwriter/php_xmlwriter.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1001,7 +1001,8 @@ static void php_xmlwriter_flush(INTERNAL_FUNCTION_PARAMETERS, int force_string)
10011001
}
10021002
output_bytes = xmlTextWriterFlush(ptr);
10031003
if (buffer) {
1004-
RETVAL_STRING((char *) buffer->content);
1004+
const xmlChar *content = xmlBufferContent(buffer);
1005+
RETVAL_STRING((const char *) content);
10051006
if (empty) {
10061007
xmlBufferEmpty(buffer);
10071008
}

0 commit comments

Comments
 (0)