Skip to content

[Yaml] prevent notice for invalid octal numbers on PHP 7.4 #36690

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/Symfony/Component/Yaml/Inline.php
Original file line number Diff line number Diff line change
Expand Up @@ -759,15 +759,21 @@ private static function evaluateScalar($scalar, $flags, $references = [])

switch (true) {
case ctype_digit($scalar):
$raw = $scalar;
if ('0' === $scalar[0]) {
return octdec(preg_replace('/[^0-7]/', '', $scalar));
}

$cast = (int) $scalar;

return '0' == $scalar[0] ? octdec($scalar) : (((string) $raw == (string) $cast) ? $cast : $raw);
return ($scalar === (string) $cast) ? $cast : $scalar;
case '-' === $scalar[0] && ctype_digit(substr($scalar, 1)):
$raw = $scalar;
if ('0' === $scalar[1]) {
return -octdec(preg_replace('/[^0-7]/', '', substr($scalar, 1)));
}

$cast = (int) $scalar;

return '0' == $scalar[1] ? -octdec(substr($scalar, 1)) : (($raw === (string) $cast) ? $cast : $raw);
return ($scalar === (string) $cast) ? $cast : $scalar;
case is_numeric($scalar):
case Parser::preg_match(self::getHexRegex(), $scalar):
$scalar = str_replace('_', '', $scalar);
Expand Down
10 changes: 10 additions & 0 deletions src/Symfony/Component/Yaml/Tests/InlineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -842,4 +842,14 @@ public function phpConstTagWithEmptyValueProvider()
[['' => 'foo', 'bar' => 'ccc'], '{!php/const : foo, bar: ccc}'],
];
}

public function testParsePositiveOctalNumberContainingInvalidDigits()
{
self::assertSame(342391, Inline::parse('0123456789'));
}

public function testParseNegativeOctalNumberContainingInvalidDigits()
{
self::assertSame(-342391, Inline::parse('-0123456789'));
}
}