Skip to content

Commit ae83d6a

Browse files
authored
Fix issues related to libxml2 2.12.0 (#12802)
* Avoid passing NULL to xmlSwitchToEncoding This otherwise switches to UTF-8 on libxml2 2.12.0 * Split tests for different error reporting behaviour in libxml2 2.12.0 * Avoid deprecation warnings for libxml2 2.12.0 We can't fully get rid of the parser globals as there are still APIs that implicitly use them. * Temporarily disable part of test for libxml 2.12.0 regression See https://gitlab.gnome.org/GNOME/libxml2/-/issues/634 * Review fixes * [ci skip] Update test description
1 parent e3de478 commit ae83d6a

14 files changed

+158
-19
lines changed

ext/dom/document.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1240,7 +1240,10 @@ xmlDocPtr dom_document_parser(zval *id, dom_load_mode mode, const char *source,
12401240
return(NULL);
12411241
}
12421242

1243-
(void) xmlSwitchToEncoding(ctxt, encoding);
1243+
if (encoding != NULL) {
1244+
/* Note: libxml 2.12+ doesn't handle NULL encoding well. */
1245+
(void) xmlSwitchToEncoding(ctxt, encoding);
1246+
}
12441247

12451248
/* If loading from memory, we need to set the base directory for the document */
12461249
if (mode != DOM_LOAD_FILE) {

ext/dom/tests/DOMDocument_loadXML_error1.phpt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
--TEST--
22
Test DOMDocument::loadXML() detects not-well formed XML
3+
--SKIPIF--
4+
<?php
5+
if (LIBXML_VERSION >= 21200) die('skip libxml2 test variant for version < 2.12');
6+
?>
37
--DESCRIPTION--
48
This test verifies the method detects an opening and ending tag mismatch
59
Environment variables used in the test:
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--TEST--
2+
Test DOMDocument::loadXML() detects not-well formed XML
3+
--SKIPIF--
4+
<?php
5+
if (LIBXML_VERSION < 21200) die('skip libxml2 test variant for version >= 2.12');
6+
?>
7+
--DESCRIPTION--
8+
This test verifies the method detects an opening and ending tag mismatch
9+
Environment variables used in the test:
10+
- XML_FILE: the xml file to load
11+
- LOAD_OPTIONS: the second parameter to pass to the method
12+
- EXPECTED_RESULT: the expected result
13+
--CREDITS--
14+
Antonio Diaz Ruiz <dejalatele@gmail.com>
15+
--EXTENSIONS--
16+
dom
17+
--ENV--
18+
XML_FILE=/not_well_formed.xml
19+
LOAD_OPTIONS=0
20+
EXPECTED_RESULT=0
21+
--FILE_EXTERNAL--
22+
domdocumentloadxml_test_method.inc
23+
--EXPECTF--
24+
Warning: DOMDocument::load%r(XML){0,1}%r(): Opening and ending tag mismatch: title line 5 and book %s
25+
26+
Warning: DOMDocument::load%r(XML){0,1}%r(): %rexpected '>'|Opening and ending tag mismatch: book line (4|5) and books%r %s

ext/dom/tests/DOMDocument_loadXML_error2_gte2_11.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Test DOMDocument::loadXML() detects not-well formed XML
33
--SKIPIF--
44
<?php
5-
if (LIBXML_VERSION < 21100) die('skip libxml2 test variant for version >= 2.11');
5+
if (LIBXML_VERSION < 21100 || LIBXML_VERSION >= 21200) die('skip libxml2 test variant for version >= 2.11 && <= 2.12');
66
?>
77
--DESCRIPTION--
88
This test verifies the method detects attributes values not closed between " or '
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--TEST--
2+
Test DOMDocument::loadXML() detects not-well formed XML
3+
--SKIPIF--
4+
<?php
5+
if (LIBXML_VERSION < 21200) die('skip libxml2 test variant for version >= 2.12');
6+
?>
7+
--DESCRIPTION--
8+
This test verifies the method detects attributes values not closed between " or '
9+
Environment variables used in the test:
10+
- XML_FILE: the xml file to load
11+
- LOAD_OPTIONS: the second parameter to pass to the method
12+
- EXPECTED_RESULT: the expected result
13+
--CREDITS--
14+
Antonio Diaz Ruiz <dejalatele@gmail.com>
15+
--EXTENSIONS--
16+
dom
17+
--ENV--
18+
XML_FILE=/not_well_formed2.xml
19+
LOAD_OPTIONS=0
20+
EXPECTED_RESULT=0
21+
--FILE_EXTERNAL--
22+
domdocumentloadxml_test_method.inc
23+
--EXPECTF--
24+
Warning: DOMDocument::loadXML(): AttValue: " or ' expected in Entity, line: 4 in %s on line %d
25+
26+
Warning: DOMDocument::loadXML(): internal error: xmlParseStartTag: problem parsing attributes in Entity, line: 4 in %s on line %d
27+
28+
Warning: DOMDocument::loadXML(): Couldn't find end of Start Tag book line 4 in Entity, line: 4 in %s on line %d
29+
30+
Warning: DOMDocument::loadXML(): Opening and ending tag mismatch: books line 3 and book in Entity, line: 7 in %s on line %d

ext/dom/tests/DOMDocument_load_error1.phpt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
--TEST--
22
Test DOMDocument::load() detects not-well formed XML
3+
--SKIPIF--
4+
<?php
5+
if (LIBXML_VERSION >= 21200) die('skip libxml2 test variant for version < 2.12');
6+
?>
37
--DESCRIPTION--
48
This test verifies the method detects an opening and ending tag mismatch
59
Environment variables used in the test:
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--TEST--
2+
Test DOMDocument::load() detects not-well formed XML
3+
--SKIPIF--
4+
<?php
5+
if (LIBXML_VERSION < 21200) die('skip libxml2 test variant for version >= 2.12');
6+
?>
7+
--DESCRIPTION--
8+
This test verifies the method detects an opening and ending tag mismatch
9+
Environment variables used in the test:
10+
- XML_FILE: the xml file to load
11+
- LOAD_OPTIONS: the second parameter to pass to the method
12+
- EXPECTED_RESULT: the expected result
13+
--CREDITS--
14+
Antonio Diaz Ruiz <dejalatele@gmail.com>
15+
--EXTENSIONS--
16+
dom
17+
--ENV--
18+
XML_FILE=/not_well_formed.xml
19+
LOAD_OPTIONS=0
20+
EXPECTED_RESULT=0
21+
--FILE_EXTERNAL--
22+
domdocumentload_test_method.inc
23+
--EXPECTF--
24+
Warning: DOMDocument::load%r(XML){0,1}%r(): Opening and ending tag mismatch: title line 5 and book %s
25+
26+
Warning: DOMDocument::load%r(XML){0,1}%r(): %rexpected '>'|Opening and ending tag mismatch: book line (4|5) and books%r %s

ext/dom/tests/DOMDocument_load_error2_gte2_11.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Test DOMDocument::load() detects not-well formed
33
--SKIPIF--
44
<?php
5-
if (LIBXML_VERSION < 21100) die('skip libxml2 test variant for version >= 2.11');
5+
if (LIBXML_VERSION < 21100 || LIBXML_VERSION >= 21200) die('skip libxml2 test variant for version >= 2.11 && <= 2.12');
66
?>
77
--DESCRIPTION--
88
This test verifies the method detects attributes values not closed between " or '
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--TEST--
2+
Test DOMDocument::load() detects not-well formed
3+
--SKIPIF--
4+
<?php
5+
if (LIBXML_VERSION < 21200) die('skip libxml2 test variant for version >= 2.12');
6+
?>
7+
--DESCRIPTION--
8+
This test verifies the method detects attributes values not closed between " or '
9+
Environment variables used in the test:
10+
- XML_FILE: the xml file to load
11+
- LOAD_OPTIONS: the second parameter to pass to the method
12+
- EXPECTED_RESULT: the expected result
13+
--CREDITS--
14+
Antonio Diaz Ruiz <dejalatele@gmail.com>
15+
--EXTENSIONS--
16+
dom
17+
--ENV--
18+
XML_FILE=/not_well_formed2.xml
19+
LOAD_OPTIONS=0
20+
EXPECTED_RESULT=0
21+
--FILE_EXTERNAL--
22+
domdocumentload_test_method.inc
23+
--EXPECTF--
24+
Warning: DOMDocument::load(): AttValue: " or ' expected in %s on line %d
25+
26+
Warning: DOMDocument::load(): internal error: xmlParseStartTag: problem parsing attributes in %s on line %d
27+
28+
Warning: DOMDocument::load(): Couldn't find end of Start Tag book line 4 in %s on line %d
29+
30+
Warning: DOMDocument::load(): Opening and ending tag mismatch: books line 3 and book in %s on line %d

ext/dom/tests/DOMNode_isEqualNode.phpt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
DOMNode::isEqualNode()
33
--EXTENSIONS--
44
dom
5+
--SKIPIF--
6+
<?php
7+
if (LIBXML_VERSION >= 21200 && LIBXML_VERSION <= 21201) {
8+
die("xfail Broken for libxml2 2.12.0 - 2.12.1 see https://gitlab.gnome.org/GNOME/libxml2/-/issues/634");
9+
}
10+
?>
511
--FILE--
612
<?php
713

ext/libxml/libxml.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,11 @@ static void _php_libxml_free_error(void *ptr)
580580
xmlResetError((xmlErrorPtr) ptr);
581581
}
582582

583-
static void _php_list_set_error_structure(xmlErrorPtr error, const char *msg, int line, int column)
583+
#if LIBXML_VERSION >= 21200
584+
static void _php_list_set_error_structure(const xmlError *error, const char *msg, int line, int column)
585+
#else
586+
static void _php_list_set_error_structure(xmlError *error, const char *msg, int line, int column)
587+
#endif
584588
{
585589
xmlError error_copy;
586590
int ret;
@@ -824,7 +828,10 @@ PHP_LIBXML_API void php_libxml_pretend_ctx_error_ex(const char *file, int line,
824828
if (!last->file) {
825829
last->file = strdup(file);
826830
}
831+
/* Until there is a replacement */
832+
ZEND_DIAGNOSTIC_IGNORED_START("-Wdeprecated-declarations")
827833
xmlCopyError(last, &xmlLastError);
834+
ZEND_DIAGNOSTIC_IGNORED_END
828835
}
829836
}
830837
}
@@ -845,11 +852,13 @@ PHP_LIBXML_API void php_libxml_ctx_warning(void *ctx, const char *msg, ...)
845852
va_end(args);
846853
}
847854

855+
#if LIBXML_VERSION >= 21200
856+
static void php_libxml_structured_error_handler(void *userData, const xmlError *error)
857+
#else
848858
static void php_libxml_structured_error_handler(void *userData, xmlErrorPtr error)
859+
#endif
849860
{
850861
_php_list_set_error_structure(error, NULL, 0, 0);
851-
852-
return;
853862
}
854863

855864
PHP_LIBXML_API void php_libxml_error_handler(void *ctx, const char *msg, ...)
@@ -1077,11 +1086,9 @@ PHP_FUNCTION(libxml_use_internal_errors)
10771086
/* {{{ Retrieve last error from libxml */
10781087
PHP_FUNCTION(libxml_get_last_error)
10791088
{
1080-
xmlErrorPtr error;
1081-
10821089
ZEND_PARSE_PARAMETERS_NONE();
10831090

1084-
error = xmlGetLastError();
1091+
const xmlError* error = xmlGetLastError();
10851092

10861093
if (error) {
10871094
object_init_ex(return_value, libxmlerror_class_entry);
@@ -1108,7 +1115,6 @@ PHP_FUNCTION(libxml_get_last_error)
11081115
/* {{{ Retrieve array of errors */
11091116
PHP_FUNCTION(libxml_get_errors)
11101117
{
1111-
11121118
xmlErrorPtr error;
11131119

11141120
ZEND_PARSE_PARAMETERS_NONE();

ext/libxml/php_libxml.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,23 +160,27 @@ ZEND_TSRMLS_CACHE_EXTERN()
160160
* See libxml2 globals.c and parserInternals.c.
161161
* The unique_name argument allows multiple sanitizes and restores within the
162162
* same function, even nested is necessary. */
163-
#define PHP_LIBXML_SANITIZE_GLOBALS(unique_name) \
163+
# define PHP_LIBXML_SANITIZE_GLOBALS(unique_name) \
164+
ZEND_DIAGNOSTIC_IGNORED_START("-Wdeprecated-declarations") \
164165
int xml_old_loadsubset_##unique_name = xmlLoadExtDtdDefaultValue; \
165166
xmlLoadExtDtdDefaultValue = 0; \
166167
int xml_old_validate_##unique_name = xmlDoValidityCheckingDefaultValue; \
167168
xmlDoValidityCheckingDefaultValue = 0; \
168169
int xml_old_pedantic_##unique_name = xmlPedanticParserDefault(0); \
169170
int xml_old_substitute_##unique_name = xmlSubstituteEntitiesDefault(0); \
170171
int xml_old_linenrs_##unique_name = xmlLineNumbersDefault(0); \
171-
int xml_old_blanks_##unique_name = xmlKeepBlanksDefault(1);
172+
int xml_old_blanks_##unique_name = xmlKeepBlanksDefault(1); \
173+
ZEND_DIAGNOSTIC_IGNORED_END
172174

173-
#define PHP_LIBXML_RESTORE_GLOBALS(unique_name) \
175+
# define PHP_LIBXML_RESTORE_GLOBALS(unique_name) \
176+
ZEND_DIAGNOSTIC_IGNORED_START("-Wdeprecated-declarations") \
174177
xmlLoadExtDtdDefaultValue = xml_old_loadsubset_##unique_name; \
175178
xmlDoValidityCheckingDefaultValue = xml_old_validate_##unique_name; \
176179
(void) xmlPedanticParserDefault(xml_old_pedantic_##unique_name); \
177180
(void) xmlSubstituteEntitiesDefault(xml_old_substitute_##unique_name); \
178181
(void) xmlLineNumbersDefault(xml_old_linenrs_##unique_name); \
179-
(void) xmlKeepBlanksDefault(xml_old_blanks_##unique_name);
182+
(void) xmlKeepBlanksDefault(xml_old_blanks_##unique_name); \
183+
ZEND_DIAGNOSTIC_IGNORED_END
180184

181185
/* Alternative for above, working directly on the context and not setting globals.
182186
* Generally faster because no locking is involved, and this has the advantage that it sets the options to a known good value. */

ext/xml/tests/bug81351.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ $code = xml_get_error_code($parser);
2121
$error = xml_error_string($code);
2222
echo "xml_parse returned $success, xml_get_error_code = $code, xml_error_string = $error\r\n";
2323
?>
24-
--EXPECT--
24+
--EXPECTF--
2525
xml_parse returned 1, xml_get_error_code = 0, xml_error_string = No error
26-
xml_parse returned 0, xml_get_error_code = 5, xml_error_string = Invalid document end
26+
%rxml_parse returned 0, xml_get_error_code = 5, xml_error_string = Invalid document end|xml_parse returned 0, xml_get_error_code = 77, xml_error_string = Tag not finished%r

ext/xml/tests/xml_error_string_basic.phpt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ foreach ($xmls as $xml) {
2121
xml_parser_free($xml_parser);
2222
}
2323
?>
24-
--EXPECT--
25-
int(5)
26-
string(20) "Invalid document end"
24+
--EXPECTF--
25+
int(%r5|77%r)
26+
string(%d) %r"Invalid document end"|"Tag not finished"%r
2727
int(47)
2828
string(35) "Processing Instruction not finished"
2929
int(57)

0 commit comments

Comments
 (0)