Skip to content

Commit 6c82ca2

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 5b2d80b commit 6c82ca2

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

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
@@ -622,7 +622,7 @@ void _xml_startElementHandler(void *userData, const XML_Char *name, const XML_Ch
622622
zval_ptr_dtor(&retval);
623623
}
624624

625-
if (!Z_ISUNDEF(parser->data)) {
625+
if (!Z_ISUNDEF(parser->data) && !EG(exception)) {
626626
if (parser->level <= XML_MAXLEVEL) {
627627
zval tag, atr;
628628
int atcnt = 0;
@@ -693,7 +693,7 @@ void _xml_endElementHandler(void *userData, const XML_Char *name)
693693
zval_ptr_dtor(&retval);
694694
}
695695

696-
if (!Z_ISUNDEF(parser->data)) {
696+
if (!Z_ISUNDEF(parser->data) && !EG(exception)) {
697697
zval tag;
698698

699699
if (parser->lastwasopen) {
@@ -741,7 +741,7 @@ void _xml_characterDataHandler(void *userData, const XML_Char *s, int len)
741741
zval_ptr_dtor(&retval);
742742
}
743743

744-
if (Z_ISUNDEF(parser->data)) {
744+
if (Z_ISUNDEF(parser->data) || EG(exception)) {
745745
return;
746746
}
747747

0 commit comments

Comments
 (0)