Skip to content

Commit 7cd23ae

Browse files
committed
Trampo test + fix bug
1 parent 3f91365 commit 7cd23ae

File tree

2 files changed

+103
-7
lines changed

2 files changed

+103
-7
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
--TEST--
2+
Test xml_set_element_handler handlers as trampoline callback
3+
--EXTENSIONS--
4+
xml
5+
--FILE--
6+
<?php
7+
8+
class CustomXmlParser
9+
{
10+
public function startHandler($XmlParser, $tag, $attr)
11+
{
12+
echo 'Method start handler: ', $tag, PHP_EOL;
13+
}
14+
15+
public function endHandler($XmlParser, $tag)
16+
{
17+
echo 'Method end handler: ', $tag, PHP_EOL;
18+
}
19+
}
20+
21+
$customParser = new CustomXmlParser;
22+
23+
class TrampolineTest {
24+
public function __call(string $name, array $arguments) {
25+
echo 'Trampoline for ', $name, PHP_EOL;
26+
echo 'Tag: ', $arguments[1], PHP_EOL;
27+
}
28+
}
29+
30+
$o = new TrampolineTest();
31+
$startCallback = [$o, 'start_handler'];
32+
$endCallback = [$o, 'end_handler'];
33+
34+
$xml = <<<HERE
35+
<a>
36+
<b/>
37+
<c>Text</c>
38+
</a>
39+
HERE;
40+
41+
echo "Both handlers are trampolines:\n";
42+
$parser = xml_parser_create();
43+
xml_set_element_handler($parser, $startCallback, $endCallback);
44+
xml_parse($parser, $xml, true);
45+
xml_parser_free($parser);
46+
47+
echo "\nStart handler is trampoline, end handler method string:\n";
48+
$parser = xml_parser_create();
49+
xml_set_object($parser, $customParser);
50+
xml_set_element_handler($parser, $startCallback, 'endHandler');
51+
xml_parse($parser, $xml, true);
52+
xml_parser_free($parser);
53+
54+
echo "\nEnd handler is trampoline, start handler method string:\n";
55+
$parser = xml_parser_create();
56+
xml_set_object($parser, $customParser);
57+
xml_set_element_handler($parser, 'startHandler', $endCallback);
58+
xml_parse($parser, $xml, true);
59+
xml_parser_free($parser);
60+
61+
?>
62+
--EXPECT--
63+
Both handlers are trampolines:
64+
Trampoline for start_handler
65+
Tag: A
66+
Trampoline for start_handler
67+
Tag: B
68+
Trampoline for end_handler
69+
Tag: B
70+
Trampoline for start_handler
71+
Tag: C
72+
Trampoline for end_handler
73+
Tag: C
74+
Trampoline for end_handler
75+
Tag: A
76+
77+
Start handler is trampoline, end handler method string:
78+
Trampoline for start_handler
79+
Tag: A
80+
Trampoline for start_handler
81+
Tag: B
82+
Method end handler: B
83+
Trampoline for start_handler
84+
Tag: C
85+
Method end handler: C
86+
Method end handler: A
87+
88+
End handler is trampoline, start handler method string:
89+
Method start handler: A
90+
Method start handler: B
91+
Trampoline for end_handler
92+
Tag: B
93+
Method start handler: C
94+
Trampoline for end_handler
95+
Tag: C
96+
Trampoline for end_handler
97+
Tag: A

ext/xml/xml.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,7 +1114,6 @@ PHP_FUNCTION(xml_set_element_handler)
11141114
zend_string *start_method_name = NULL;
11151115
zend_string *end_method_name = NULL;
11161116

1117-
// TODO: cover trampolines with tests, as the !ZEND_FCC_INITIALIZED branches are never executed right now
11181117
if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "Of!f!", &pind, xml_parser_ce, &start_fci, &start_fcc, &end_fci, &end_fcc) == SUCCESS) {
11191118
parser = Z_XMLPARSER_P(pind);
11201119
if (ZEND_FCI_INITIALIZED(start_fci) && !ZEND_FCC_INITIALIZED(start_fcc)) {
@@ -1132,9 +1131,9 @@ PHP_FUNCTION(xml_set_element_handler)
11321131
} else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "Of!S", &pind, xml_parser_ce, &start_fci, &start_fcc, &end_method_name) == SUCCESS) {
11331132
parser = Z_XMLPARSER_P(pind);
11341133

1135-
bool status = php_xml_check_string_method_arg(3, parser, end_method_name, &start_fcc);
1134+
bool status = php_xml_check_string_method_arg(3, parser, end_method_name, &end_fcc);
11361135
if (status == false) {
1137-
RETURN_FALSE;
1136+
RETURN_THROWS();
11381137
}
11391138

11401139
if (ZEND_FCI_INITIALIZED(start_fci) && !ZEND_FCC_INITIALIZED(start_fcc)) {
@@ -1148,7 +1147,7 @@ PHP_FUNCTION(xml_set_element_handler)
11481147

11491148
bool status = php_xml_check_string_method_arg(2, parser, start_method_name, &start_fcc);
11501149
if (status == false) {
1151-
RETURN_FALSE;
1150+
RETURN_THROWS();
11521151
}
11531152

11541153
if (ZEND_FCI_INITIALIZED(end_fci) && !ZEND_FCC_INITIALIZED(end_fcc)) {
@@ -1162,11 +1161,11 @@ PHP_FUNCTION(xml_set_element_handler)
11621161

11631162
bool status = php_xml_check_string_method_arg(2, parser, start_method_name, &start_fcc);
11641163
if (status == false) {
1165-
RETURN_FALSE;
1164+
RETURN_THROWS();
11661165
}
11671166
status = php_xml_check_string_method_arg(3, parser, end_method_name, &end_fcc);
11681167
if (status == false) {
1169-
RETURN_FALSE;
1168+
RETURN_THROWS();
11701169
}
11711170
} else {
11721171
zval *dummy_start;
@@ -1223,7 +1222,7 @@ static void php_xml_set_handler_parse_callable(
12231222

12241223
bool status = php_xml_check_string_method_arg(2, *parser, method_name, parser_handler_fcc);
12251224
if (status == false) {
1226-
RETURN_FALSE;
1225+
RETURN_THROWS();
12271226
}
12281227
} else {
12291228
zval *dummy;

0 commit comments

Comments
 (0)