Skip to content

Commit 7626e88

Browse files
committed
Fix phpGH-16892: ini_parse_quantity() fails to parse inputs starting with 0x0b
1 parent 2c26772 commit 7626e88

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

Zend/tests/zend_ini/gh16892.phpt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--TEST--
2+
GH-16892 (ini_parse_quantity() fails to parse inputs starting with 0x0b)
3+
--FILE--
4+
<?php
5+
echo ini_parse_quantity('0x0b'), "\n";
6+
echo ini_parse_quantity('0xb'), "\n";
7+
echo ini_parse_quantity('-0x0B'), "\n";
8+
echo ini_parse_quantity('-0xB'), "\n";
9+
echo ini_parse_quantity('0x0beef'), "\n";
10+
echo ini_parse_quantity('0xbeef'), "\n";
11+
echo ini_parse_quantity('-0x0BEEF'), "\n";
12+
echo ini_parse_quantity('-0xBEEF'), "\n";
13+
?>
14+
--EXPECT--
15+
11
16+
11
17+
-11
18+
-11
19+
48879
20+
48879
21+
-48879
22+
-48879

Zend/zend_ini.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ typedef enum {
587587
ZEND_INI_PARSE_QUANTITY_UNSIGNED,
588588
} zend_ini_parse_quantity_signed_result_t;
589589

590-
static const char *zend_ini_consume_quantity_prefix(const char *const digits, const char *const str_end) {
590+
static const char *zend_ini_consume_quantity_prefix(const char *const digits, const char *const str_end, int base) {
591591
const char *digits_consumed = digits;
592592
/* Ignore leading whitespace. */
593593
while (digits_consumed < str_end && zend_is_whitespace(*digits_consumed)) {++digits_consumed;}
@@ -606,9 +606,14 @@ static const char *zend_ini_consume_quantity_prefix(const char *const digits, co
606606
case 'X':
607607
case 'o':
608608
case 'O':
609+
digits_consumed += 2;
610+
break;
609611
case 'b':
610612
case 'B':
611-
digits_consumed += 2;
613+
if (base != 16) {
614+
/* 0b or 0B is valid in base 16, but not in the other supported bases. */
615+
digits_consumed += 2;
616+
}
612617
break;
613618
}
614619
}
@@ -696,7 +701,7 @@ static zend_ulong zend_ini_parse_quantity_internal(zend_string *value, zend_ini_
696701
return 0;
697702
}
698703
digits += 2;
699-
if (UNEXPECTED(digits == str_end || digits != zend_ini_consume_quantity_prefix(digits, str_end))) {
704+
if (UNEXPECTED(digits == str_end || digits != zend_ini_consume_quantity_prefix(digits, str_end, base))) {
700705
/* Escape the string to avoid null bytes and to make non-printable chars
701706
* visible */
702707
smart_str_append_escaped(&invalid, ZSTR_VAL(value), ZSTR_LEN(value));

0 commit comments

Comments
 (0)