Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8f2cd5b

Browse files
committed
bug #34812 [Yaml] fix parsing negative octal numbers (xabbuh)
This PR was merged into the 3.4 branch. Discussion ---------- [Yaml] fix parsing negative octal numbers | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | | License | MIT | Doc PR | Commits ------- 7ab53f9 fix parsing negative octal numbers
2 parents ae6c5d3 + 7ab53f9 commit 8f2cd5b

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

src/Symfony/Component/Yaml/Inline.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -735,11 +735,11 @@ private static function evaluateScalar($scalar, $flags, $references = [])
735735
// Optimize for returning strings.
736736
// no break
737737
case '+' === $scalar[0] || '-' === $scalar[0] || '.' === $scalar[0] || is_numeric($scalar[0]):
738+
if (Parser::preg_match('{^[+-]?[0-9][0-9_]*$}', $scalar)) {
739+
$scalar = str_replace('_', '', (string) $scalar);
740+
}
741+
738742
switch (true) {
739-
case Parser::preg_match('{^[+-]?[0-9][0-9_]*$}', $scalar):
740-
$scalar = str_replace('_', '', (string) $scalar);
741-
// omitting the break / return as integers are handled in the next case
742-
// no break
743743
case ctype_digit($scalar):
744744
$raw = $scalar;
745745
$cast = (int) $scalar;
@@ -749,7 +749,7 @@ private static function evaluateScalar($scalar, $flags, $references = [])
749749
$raw = $scalar;
750750
$cast = (int) $scalar;
751751

752-
return '0' == $scalar[1] ? octdec($scalar) : (((string) $raw === (string) $cast) ? $cast : $raw);
752+
return '0' == $scalar[1] ? -octdec(substr($scalar, 1)) : (($raw === (string) $cast) ? $cast : $raw);
753753
case is_numeric($scalar):
754754
case Parser::preg_match(self::getHexRegex(), $scalar):
755755
$scalar = str_replace('_', '', $scalar);

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -781,4 +781,20 @@ public function testUnfinishedInlineMap()
781781
$this->expectExceptionMessage('Unexpected end of line, expected one of ",}" at line 1 (near "{abc: \'def\'").');
782782
Inline::parse("{abc: 'def'");
783783
}
784+
785+
/**
786+
* @dataProvider getTestsForOctalNumbers
787+
*/
788+
public function testParseOctalNumbers($expected, $yaml)
789+
{
790+
self::assertSame($expected, Inline::parse($yaml));
791+
}
792+
793+
public function getTestsForOctalNumbers()
794+
{
795+
return [
796+
'positive octal number' => [28, '034'],
797+
'negative octal number' => [-28, '-034'],
798+
];
799+
}
784800
}

0 commit comments

Comments
 (0)