8000 [Yaml][Inline] Fail properly on empty object tag and empty const tag · symfony/symfony@1e9c967 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1e9c967

Browse files
committed
[Yaml][Inline] Fail properly on empty object tag and empty const tag
1 parent db3134e commit 1e9c967

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

src/Symfony/Component/Yaml/Inline.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,11 @@ private static function evaluateScalar($scalar, $flags, $references = [])
692692
return null;
693693
case 0 === strpos($scalar, '!php/object'):
694694
if (self::$objectSupport) {
695-
return unserialize(self::parseScalar(substr($scalar, 12)));
695+
if (false === $str = substr($scalar, 12)) {
696+
throw new ParseException('The !php/object tag requires a value.', self::$parsedLineNumber + 1, $scalar, self::$parsedFilename);
697+
}
698+
699+
return unserialize(self::parseScalar($str));
696700
}
697701

698702
if (self::$exceptionOnInvalidType) {
@@ -717,8 +721,12 @@ private static function evaluateScalar($scalar, $flags, $references = [])
717721
return null;
718722
case 0 === strpos($scalar, '!php/const'):
719723
if (self::$constantSupport) {
724+
if (false === $const = substr($scalar, 11)) {
725+
throw new ParseException('The !php/const tag requires a value.', self::$parsedLineNumber + 1, $scalar, self::$parsedFilename);
726+
}
727+
720728
$i = 0;
721-
if (\defined($const = self::parseScalar(substr($scalar, 11), 0, null, $i, false))) {
729+
if (\defined($const = self::parseScalar($const, 0, null, $i, false))) {
722730
return \constant($const);
723731
}
724732

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -799,4 +799,31 @@ public function getTestsForOctalNumbers()
799799
'negative octal number' => [-28, '-034'],
800800
];
801801
}
802+
803+
/**
804+
* @dataProvider tagThrowsOnEmptyProvider
805+
*/
806+
public function testTagThrowsOnEmpty($tag, $inMapping, $flags)
807+
{
808+
$this->expectException(ParseException::class);
809+
$this->expectExceptionMessage(sprintf('The %s tag requires a value at line 1 (near "%s").', $tag, $tag));
810+
811+
if ($inMapping) {
812+
$value = sprintf('{%s : bar}', $tag);
813+
} else {
814+
$value = $tag.' ';
815+
}
816+
817+
Inline::parse($value, $flags);
818+
}
819+
820+
public function tagThrowsOnEmptyProvider()
821+
{
822+
return [
823+
['!php/object', false, Yaml::PARSE_OBJECT],
824+
['!php/object', true, Yaml::PARSE_OBJECT],
825+
['!php/const', false, Yaml::PARSE_CONSTANT],
826+
['!php/const', true, Yaml::PARSE_CONSTANT],
827+
];
828+
}
802829
}

0 commit comments

Comments
 (0)
0