diff --git a/src/Symfony/Component/Yaml/Parser.php b/src/Symfony/Component/Yaml/Parser.php index dadf7df446bcb..19b48cfe38185 100644 --- a/src/Symfony/Component/Yaml/Parser.php +++ b/src/Symfony/Component/Yaml/Parser.php @@ -1158,7 +1158,18 @@ private function lexInlineQuotedString(int &$cursor = 0): string private function lexUnquotedString(int &$cursor): string { $offset = $cursor; - $cursor += strcspn($this->currentLine, '[]{},:', $cursor); + + while ($cursor < strlen($this->currentLine)) { + if (in_array($this->currentLine[$cursor], ['[', ']', '{', '}', ',', ':'], true)) { + break; + } + + if (\in_array($this->currentLine[$cursor], [' ', "\t"], true) && '#' === ($this->currentLine[$cursor + 1] ?? '')) { + break; + } + + ++$cursor; + } if ($cursor === $offset) { throw new ParseException('Malformed unquoted YAML string.'); @@ -1235,7 +1246,7 @@ private function consumeWhitespaces(int &$cursor): bool $whitespacesConsumed = 0; do { - $whitespaceOnlyTokenLength = strspn($this->currentLine, ' ', $cursor); + $whitespaceOnlyTokenLength = strspn($this->currentLine, " \t", $cursor); $whitespacesConsumed += $whitespaceOnlyTokenLength; $cursor += $whitespaceOnlyTokenLength; diff --git a/src/Symfony/Component/Yaml/Tests/ParserTest.php b/src/Symfony/Component/Yaml/Tests/ParserTest.php index 7725ac8d4d61c..c1f643f43603d 100644 --- a/src/Symfony/Component/Yaml/Tests/ParserTest.php +++ b/src/Symfony/Component/Yaml/Tests/ParserTest.php @@ -1751,6 +1751,69 @@ public function testParseMultiLineUnquotedString() $this->assertSame(['foo' => 'bar baz foobar foo', 'bar' => 'baz'], $this->parser->parse($yaml)); } + /** + * @dataProvider unquotedStringWithTrailingComment + */ + public function testParseMultiLineUnquotedStringWithTrailingComment(string $yaml, array $expected) + { + $this->assertSame($expected, $this->parser->parse($yaml)); + } + + public function unquotedStringWithTrailingComment() + { + return [ + 'comment after comma' => [ + <<<'YAML' + { + foo: 3, # comment + bar: 3 + } + YAML, + ['foo' => 3, 'bar' => 3], + ], + 'comment after space' => [ + <<<'YAML' + { + foo: 3 # comment + } + YAML, + ['foo' => 3], + ], + 'comment after space, but missing space after #' => [ + <<<'YAML' + { + foo: 3 #comment + } + YAML, + ['foo' => 3], + ], + 'comment after tab' => [ + << 3], + ], + 'comment after tab, but missing space after #' => [ + << 3], + ], + '# in mapping value' => [ + <<<'YAML' + { + foo: example.com/#about + } + YAML, + ['foo' => 'example.com/#about'], + ], + ]; + } + /** * @dataProvider escapedQuotationCharactersInQuotedStrings */