8000 bug #35364 [Yaml] Throw on unquoted exclamation mark (fancyweb) · symfony/symfony@79d9bda · GitHub
[go: up one dir, main page]

Skip to content

Commit 79d9bda

Browse files
bug #35364 [Yaml] Throw on unquoted exclamation mark (fancyweb)
This PR was merged into the 4.3 branch. Discussion ---------- [Yaml] Throw on unquoted exclamation mark | Q | A | ------------- | --- | Branch? | 4.3 | Bug fix? | yes | New feature? | no | Deprecations? | | Tickets | #35344 | License | MIT | Doc PR | - Commits ------- 6b4147c [Yaml] Throw on unquoted exclamation mark
2 parents c7a1916 + 6b4147c commit 79d9bda

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

src/Symfony/Component/Yaml/Inline.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,10 @@ private static function parseTag(string $value, int &$i, int $flags): ?string
674674
$nextOffset = $i + $tagLength + 1;
675675
$nextOffset += strspn($value, ' ', $nextOffset);
676676

677+
if ('' === $tag && (!isset($value[$nextOffset]) || \in_array($value[$nextOffset], [']', '}', ','], true))) {
678+
throw new ParseException(sprintf('Using the unquoted scalar value "!" is not supported. You must quote it.', $value), self::$parsedLineNumber + 1, $value, self::$parsedFilename);
679+
}
680+
677681
// Is followed by a scalar and is a built-in tag
678682
if ('' !== $tag && (!isset($value[$nextOffset]) || !\in_array($value[$nextOffset], ['[', '{'], true)) && ('!' === $tag[0] || 'str' === $tag || 'php/const' === $tag || 'php/object' === $tag)) {
679683
// Manage in {@link self::evaluateScalar()}

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,4 +737,69 @@ public function getTestsForOctalNumbers()
737737
'negative octal number' => [-28, '-034'],
738738
];
739739
}
740+
741+
/**
742+
* @dataProvider unquotedExclamationMarkThrowsProvider
743+
*/
744+
public function testUnquotedExclamationMarkThrows(string $value)
745+
{
746+
$this->expectException(ParseException::class);
747+
$this->expectExceptionMessageRegExp('/^Using the unquoted scalar value "!" is not supported\. You must quote it at line 1 \(near "/');
748+
749+
Inline::parse($value);
750+
}
751+
752+
public function unquotedExclamationMarkThrowsProvider()
753+
{
754+
return [
755+
['!'],
756+
['! '],
757+
['! '],
758+
[' ! '],
759+
['[!]'],
760+
['[! ]'],
761+
['[! ]'],
762+
['[!, "foo"]'],
763+
['["foo", !, "ccc"]'],
764+
['{foo: !}'],
765+
['{foo: !}'],
766+
['{foo: !, bar: "ccc"}'],
767+
['{bar: "ccc", foo: ! }'],
768+
['!]]]'],
769+
['!}'],
770+
['!,}foo,]'],
771+
['! [!]'],
772+
];
773+
}
774+
775+
/**
776+
* @dataProvider quotedExclamationMarkProvider
777+
*/
778+
public function testQuotedExclamationMark($expected, string $value)
779+
{
780+
$this->assertSame($expected, Inline::parse($value));
781+
}
782+
783+
// This provider should stay consistent with unquotedExclamationMarkThrowsProvider
784+
public function quotedExclamationMarkProvider()
785+
{
786+
return [
787+
['!', '"!"'],
788+
['! ', '"! "'],
789+
[' !', '" !"'],
790+
[' ! ', '" ! "'],
791+
[['!'], '["!"]'],
792+
[['! '], '["! "]'],
793+
[['!', 'foo'], '["!", "foo"]'],
794+
[['foo', '!', 'ccc'], '["foo", "!", "ccc"]'],
795+
[['foo' => '!'], '{foo: "!"}'],
796+
[['foo' => ' !'], '{foo: " !"}'],
797+
[['foo' => '!', 'bar' => 'ccc'], '{foo: "!", bar: "ccc"}'],
798+
[['bar' => 'ccc', 'foo' => '! '], '{bar: "ccc", foo: "! "}'],
799+
['!]]]', '"!]]]"'],
800+
['!}', '"!}"'],
801+
['!,}foo,]', '"!,}foo,]"'],
802+
[['!'], '! ["!"]'],
803+
];
804+
}
740805
}

0 commit comments

Comments
 (0)
0