Skip to content

Commit

Permalink
Fix ini_parse_quantity()
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed Jan 9, 2024
1 parent 3f7e0b8 commit 78f63d4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 16 deletions.
36 changes: 36 additions & 0 deletions src/Php82/Php82.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,42 @@ public static function ini_parse_quantity(string $value): int

return 0;
}

$digits_consumed = $digits;
/* Ignore leading whitespace. */
while ($digits_consumed < $str_end && false !== strpos($ctype_space, $value[$digits_consumed])) {
++$digits_consumed;
}
if ($digits_consumed !== $str_end && ($value[$digits_consumed] === '+' || $value[$digits_consumed] === '-')) {
++$digits_consumed;
}

if ($value[$digits_consumed] === '0') {
/* Value is just 0 */
if ($digits_consumed + 1 === $str_end) {
return 0;
}
switch ($value[$digits_consumed + 1]) {
case 'x':
case 'X':
case 'o':
case 'O':
case 'b':
case 'B':
$digits_consumed += 2;
break;
}
}

if ($digits !== $digits_consumed) {
$message = sprintf(
'Invalid quantity "%s": no digits after base prefix, interpreting as "0" for backwards compatibility',
self::escapeString($value)
);
trigger_error($message, \E_USER_WARNING);

return 0;
}
}

evaluation:
Expand Down
19 changes: 3 additions & 16 deletions tests/Php82/Php82Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,19 +240,6 @@ public function testIniParseQuantity()
$this->assertSame(-17179869184, ini_parse_quantity('-0X10G'));
}

public function testIniParseQuantityUndocumentedFeatures()
{
$this->assertSame(18, ini_parse_quantity('0x0x12'));

$this->assertSame(2, ini_parse_quantity('0b+10'));
$this->assertSame(8, ini_parse_quantity('0o+10'));
$this->assertSame(16, ini_parse_quantity('0x+10'));

$this->assertSame(2, ini_parse_quantity('0b 10'));
$this->assertSame(8, ini_parse_quantity('0o 10'));
$this->assertSame(16, ini_parse_quantity('0x 10'));
}

public function testIniParseQuantityZeroWithMultiplier()
{
// Note that "1 K" is valid
Expand Down Expand Up @@ -314,8 +301,8 @@ public function testIniParseQuantityNoLeadingDigits()
public function testIniParseQuantityOutOfRange()
{
error_clear_last();
$this->assertSame(-4096, @ini_parse_quantity(' 0x-4K '));
$this->assertSame('Invalid quantity " 0x-4K ": value is out of range, using overflow result for backwards compatibility', error_get_last()['message']);
$this->assertSame(0, @ini_parse_quantity(' 0x-4K '));
$this->assertSame('Invalid quantity " 0x-4K ": no digits after base prefix, interpreting as "0" for backwards compatibility', error_get_last()['message']);
$this->assertContains(error_get_last()['type'], [\E_WARNING, \E_USER_WARNING]);
}

Expand All @@ -339,7 +326,7 @@ public function testIniParseQuantitySignAfterPrefixButNoDigits()
{
error_clear_last();
$this->assertSame(0, @ini_parse_quantity(' 0b- '));
$this->assertSame('Invalid quantity " 0b- ": no valid leading digits, interpreting as "0" for backwards compatibility', error_get_last()['message']);
$this->assertSame('Invalid quantity " 0b- ": no digits after base prefix, interpreting as "0" for backwards compatibility', error_get_last()['message']);
$this->assertContains(error_get_last()['type'], [\E_WARNING, \E_USER_WARNING]);
}

Expand Down

0 comments on commit 78f63d4

Please sign in to comment.