Skip to content

Commit 2c6177a

Browse files
committed
Merge branch 'PHP-7.4' into PHP-8.0
* PHP-7.4: Fix #81351: xml_parse may fail, but has no error code
2 parents 77c1c42 + 80a377e commit 2c6177a

File tree

5 files changed

+44
-9
lines changed

5 files changed

+44
-9
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ PHP NEWS
77
. Fixed bug #81346 (Non-seekable streams don't update position after write).
88
(cmb)
99

10+
- XML:
11+
. Fixed bug #81351 (xml_parse may fail, but has no error code). (cmb, Nikita)
12+
1013
26 Aug 2021, PHP 8.0.10
1114

1215
- Core:

ext/xml/compat.c

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -563,16 +563,8 @@ XML_Parse(XML_Parser parser, const XML_Char *data, int data_len, int is_final)
563563
{
564564
int error;
565565

566-
if (parser->parser->lastError.level >= XML_ERR_WARNING) {
567-
return 0;
568-
}
569-
570566
error = xmlParseChunk(parser->parser, (char *) data, data_len, is_final);
571-
if (error) {
572-
return 0;
573-
} else {
574-
return 1;
575-
}
567+
return !error && parser->parser->lastError.level <= XML_ERR_WARNING;
576568
}
577569

578570
PHP_XML_API int

ext/xml/tests/bug73135.phpt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ HERE;
2121
xml_parse($parser, $xml);
2222
?>
2323
--EXPECTF--
24+
Warning: xml_parse(): Parser must not be called recursively in %s%ebug73135.php on line %d
25+
26+
Warning: xml_parse(): Parser must not be called recursively in %s%ebug73135.php on line %d
27+
2428
Warning: xml_parse(): Unable to call handler ahihi() in %s%ebug73135.php on line %d
2529

2630
Warning: xml_parse(): Unable to call handler ahihi() in %s%ebug73135.php on line %d

ext/xml/tests/bug81351.phpt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
Bug #81351 (xml_parse may fail, but has no error code)
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded('xml')) die("skip xml extension not available");
6+
?>
7+
--FILE--
8+
<?php
9+
$xml = <<<XML
10+
<?xml version="1.0" encoding="utf-8"?>
11+
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
12+
<soap:Body>
13+
<X xmlns="example.org">
14+
XML;
15+
16+
$parser = xml_parser_create_ns('UTF-8');
17+
$success = xml_parse($parser, $xml, false);
18+
$code = xml_get_error_code($parser);
19+
$error = xml_error_string($code);
20+
echo "xml_parse returned $success, xml_get_error_code = $code, xml_error_string = $error\r\n";
21+
$success = xml_parse($parser, 'Y>', true);
22+
$code = xml_get_error_code($parser);
23+
$error = xml_error_string($code);
24+
echo "xml_parse returned $success, xml_get_error_code = $code, xml_error_string = $error\r\n";
25+
?>
26+
--EXPECT--
27+
xml_parse returned 1, xml_get_error_code = 0, xml_error_string = No error
28+
xml_parse returned 0, xml_get_error_code = 5, xml_error_string = Invalid document end

ext/xml/xml.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,6 +1257,10 @@ PHP_FUNCTION(xml_parse)
12571257
}
12581258

12591259
parser = Z_XMLPARSER_P(pind);
1260+
if (parser->isparsing) {
1261+
php_error_docref(NULL, E_WARNING, "Parser must not be called recursively");
1262+
RETURN_FALSE;
1263+
}
12601264
parser->isparsing = 1;
12611265
ret = XML_Parse(parser->parser, (XML_Char*)data, data_len, isFinal);
12621266
parser->isparsing = 0;
@@ -1304,6 +1308,10 @@ PHP_FUNCTION(xml_parse_into_struct)
13041308
XML_SetElementHandler(parser->parser, _xml_startElementHandler, _xml_endElementHandler);
13051309
XML_SetCharacterDataHandler(parser->parser, _xml_characterDataHandler);
13061310

1311+
if (parser->isparsing) {
1312+
php_error_docref(NULL, E_WARNING, "Parser must not be called recursively");
1313+
RETURN_FALSE;
1314+
}
13071315
parser->isparsing = 1;
13081316
ret = XML_Parse(parser->parser, (XML_Char*)data, data_len, 1);
13091317
parser->isparsing = 0;

0 commit comments

Comments
 (0)