Skip to content

Commit 8a775d1

Browse files
committed
ext/XML: Improve invalid value handling for XML_OPTION_SKIP_TAGSTART
1 parent 2764736 commit 8a775d1

2 files changed

Lines changed: 20 additions & 6 deletions

File tree

ext/xml/tests/xml_parser_set_option_errors.phpt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ xml_parser_set_option($xmlParser, XML_OPTION_SKIP_TAGSTART, new stdClass());
4040

4141
xml_parser_set_option($xmlParser, XML_OPTION_SKIP_TAGSTART, "not numeric");
4242

43+
xml_parser_set_option($xmlParser, XML_OPTION_SKIP_TAGSTART, "123test");
44+
4345
echo "Encodings\n";
4446
try {
4547
xml_parser_set_option($xmlParser, XML_OPTION_TARGET_ENCODING, 'Invalid Encoding');
@@ -77,7 +79,9 @@ Warning: xml_parser_set_option(): Argument #3 ($value) must be of type string|in
7779

7880
Warning: xml_parser_set_option(): Argument #3 ($value) must be of type string|int|bool, stdClass given in %s on line %d
7981

80-
Warning: xml_parser_set_option(): Argument #3 ($value) must be a valid integer for option XML_OPTION_SKIP_TAGSTART in %s on line %d
82+
Warning: xml_parser_set_option(): Argument #3 ($value) must be of type int for the XML_OPTION_SKIP_TAGSTART option, string given in %s on line %d
83+
84+
Warning: xml_parser_set_option(): Argument #3 ($value) must be of type int for the XML_OPTION_SKIP_TAGSTART option, string given in %s on line %d
8185
Encodings
8286
xml_parser_set_option(): Argument #3 ($value) is not a supported target encoding
8387

ext/xml/xml.c

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1588,15 +1588,25 @@ PHP_FUNCTION(xml_parser_set_option)
15881588
/* Integer option */
15891589
case PHP_XML_OPTION_SKIP_TAGSTART: {
15901590
/* The tag start offset is stored in an int */
1591-
bool failed = false;
1592-
zend_long value_long = zval_try_get_long(value, &failed);
1593-
if (failed) {
1594-
if (Z_TYPE_P(value) == IS_STRING) {
1591+
zend_long value_long;
1592+
1593+
if (Z_TYPE_P(value) == IS_LONG) {
1594+
value_long = Z_LVAL_P(value);
1595+
} else if (Z_TYPE_P(value) == IS_FALSE) {
1596+
value_long = 0;
1597+
} else if (Z_TYPE_P(value) == IS_TRUE) {
1598+
value_long = 1;
1599+
} else if (Z_TYPE_P(value) == IS_STRING) {
1600+
int oflow = 0;
1601+
if (is_numeric_string_ex(Z_STRVAL_P(value), Z_STRLEN_P(value), &value_long, NULL, false, &oflow, NULL) != IS_LONG || oflow) {
15951602
php_error_docref(NULL, E_WARNING,
1596-
"Argument #3 ($value) must be a valid integer for option XML_OPTION_SKIP_TAGSTART");
1603+
"Argument #3 ($value) must be of type int for the XML_OPTION_SKIP_TAGSTART option, string given");
1604+
RETURN_FALSE;
15971605
}
1606+
} else {
15981607
RETURN_FALSE;
15991608
}
1609+
16001610
if (value_long < 0 || value_long > INT_MAX) {
16011611
/* TODO Promote to ValueError in PHP 9.0 */
16021612
php_error_docref(NULL, E_WARNING, "Argument #3 ($value) must be between 0 and %d"

0 commit comments

Comments
 (0)