bug #38099 Prevent parsing invalid octal digits as octal numbers (julienfalque)

This PR was merged into the 3.4 branch.

Discussion
----------

Prevent parsing invalid octal digits as octal numbers

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | -
| License       | MIT
| Doc PR        | -

Values starting with `0` but containing `8` or `9` such as `0123456789` are not valid octal numbers, therefore they should be parsed as regular strings. This is consistent with how other invalid octal values are parsed, e.g. `01234567ab` already gets parsed as the string `"01234567ab"`.

Commits
-------

c7dcd82f03 Prevent parsing invalid octal digits as octal numbers
This commit is contained in:
Fabien Potencier 2020-09-08 06:53:29 +02:00
commit 91b6739031
2 changed files with 6 additions and 6 deletions

View File

@ -759,16 +759,16 @@ class Inline
switch (true) {
case ctype_digit($scalar):
if ('0' === $scalar[0]) {
return octdec(preg_replace('/[^0-7]/', '', $scalar));
if (preg_match('/^0[0-7]+$/', $scalar)) {
return octdec($scalar);
}
$cast = (int) $scalar;
return ($scalar === (string) $cast) ? $cast : $scalar;
case '-' === $scalar[0] && ctype_digit(substr($scalar, 1)):
if ('0' === $scalar[1]) {
return -octdec(preg_replace('/[^0-7]/', '', substr($scalar, 1)));
if (preg_match('/^-0[0-7]+$/', $scalar)) {
return -octdec(substr($scalar, 1));
}
$cast = (int) $scalar;

View File

@ -853,11 +853,11 @@ class InlineTest extends TestCase
public function testParsePositiveOctalNumberContainingInvalidDigits()
{
self::assertSame(342391, Inline::parse('0123456789'));
self::assertSame('0123456789', Inline::parse('0123456789'));
}
public function testParseNegativeOctalNumberContainingInvalidDigits()
{
self::assertSame(-342391, Inline::parse('-0123456789'));
self::assertSame('-0123456789', Inline::parse('-0123456789'));
}
}