Skip to content

Commit ac8db36

Browse files
committed
Fix GH-15868: Assertion failure in xml_parse_into_struct after exception
Upon unwinding from an exception, the parser state is not stable, we should not continue updating the values if an exception was thrown. Closes GH-15879.
1 parent 4e12189 commit ac8db36

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ PHP NEWS
2323
. Fixed bug GH-15613 (overflow on unpack call hex string repeater).
2424
(David Carlier)
2525

26+
- XML:
27+
. Fixed bug GH-15868 (Assertion failure in xml_parse_into_struct after
28+
exception). (nielsdos)
29+
2630
26 Sep 2024, PHP 8.2.24
2731

2832
- Core:

ext/xml/tests/gh15868.phpt

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
--TEST--
2+
GH-15868 (Assertion failure in xml_parse_into_struct after exception)
3+
--EXTENSIONS--
4+
xml
5+
--FILE--
6+
<?php
7+
$parser = xml_parser_create();
8+
xml_set_element_handler($parser,
9+
function ($parser, $name, $attrs) {
10+
throw new Error('stop 1');
11+
}, function ($parser, $name) {
12+
}
13+
);
14+
try {
15+
xml_parse_into_struct($parser, "<container/>", $values, $tags);
16+
} catch (Error $e) {
17+
echo $e->getMessage(), "\n";
18+
}
19+
20+
$parser = xml_parser_create();
21+
xml_set_element_handler($parser,
22+
function ($parser, $name, $attrs) {
23+
}, function ($parser, $name) {
24+
throw new Error('stop 2');
25+
}
26+
);
27+
try {
28+
xml_parse_into_struct($parser, "<container/>", $values, $tags);
29+
} catch (Error $e) {
30+
echo $e->getMessage(), "\n";
31+
}
32+
33+
$parser = xml_parser_create();
34+
xml_set_character_data_handler($parser, function() {
35+
throw new Error('stop 3');
36+
});
37+
try {
38+
xml_parse_into_struct($parser, "<root><![CDATA[x]]></root>", $values, $tags);
39+
} catch (Error $e) {
40+
echo $e->getMessage(), "\n";
41+
}
42+
?>
43+
--EXPECT--
44+
stop 1
45+
stop 2
46+
stop 3

ext/xml/xml.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,7 @@ void _xml_startElementHandler(void *userData, const XML_Char *name, const XML_Ch
628628
zval_ptr_dtor(&retval);
629629
}
630630

631-
if (!Z_ISUNDEF(parser->data)) {
631+
if (!Z_ISUNDEF(parser->data) && !EG(exception)) {
632632
if (parser->level <= XML_MAXLEVEL) {
633633
zval tag, atr;
634634
int atcnt = 0;
@@ -699,7 +699,7 @@ void _xml_endElementHandler(void *userData, const XML_Char *name)
699699
zval_ptr_dtor(&retval);
700700
}
701701

702-
if (!Z_ISUNDEF(parser->data)) {
702+
if (!Z_ISUNDEF(parser->data) && !EG(exception)) {
703703
zval tag;
704704

705705
if (parser->lastwasopen) {
@@ -747,7 +747,7 @@ void _xml_characterDataHandler(void *userData, const XML_Char *s, int len)
747747
zval_ptr_dtor(&retval);
748748
}
749749

750-
if (Z_ISUNDEF(parser->data)) {
750+
if (Z_ISUNDEF(parser->data) || EG(exception)) {
751751
return;
752752
}
753753

0 commit comments

Comments
 (0)