8000 bug #38099 Prevent parsing invalid octal digits as octal numbers (jul… · symfony/symfony@91b6739 · GitHub
[go: up one dir, main page]

Skip to content

Commit 91b6739

Browse files
committed
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 ------- c7dcd82 Prevent parsing invalid octal digits as octal numbers
2 parents 8e34978 + c7dcd82 commit 91b6739

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

src/Symfony/Component/Yaml/Inline.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -759,16 +759,16 @@ private static function evaluateScalar($scalar, $flags, $references = [])
759759

760760
switch (true) {
761761
case ctype_digit($scalar):
762-
if ('0' === $scalar[0]) {
763-
return octdec(preg_replace('/[^0-7]/', '', $scalar));
762+
if (preg_match('/^0[0-7]+$/', $scalar)) {
763+
return octdec($scalar);
764764
}
765765

766766
$cast = (int) $scalar;
767767

768768
return ($scalar === (string) $cast) ? $cast : $scalar;
769769
case '-' === $scalar[0] && ctype_digit(substr($scalar, 1)):
770-
if ('0' === $scalar[1]) {
771-
return -octdec(preg_replace('/[^0-7]/', '', substr($scalar, 1)));
770+
if (preg_match('/^-0[0-7]+$/', $scalar)) {
771+
return -octdec(substr($scalar, 1));
772772
}
773773

774774
$cast = (int) $scalar;

src/Symfony/Component/Yaml/Tests/InlineTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -853,11 +853,11 @@ public function phpConstTagWithEmptyValueProvider()
853853

854854
public function testParsePositiveOctalNumberContainingInvalidDigits()
855855
{
856-
self::assertSame(342391, Inline::parse('0123456789'));
856+ self::assertSame('0123456789', Inline::parse('0123456789'));
857857
}
858858

859859
public function testParseNegativeOctalNumberContainingInvalidDigits()
860860
{
861-
self::assertSame(-342391, Inline::parse('-0123456789'));
861+
self::assertSame('-0123456789', Inline::parse('-0123456789'));
862862
}
863863
}

0 commit comments

Comments
 (0)
0