Skip to content

Commit

Permalink
[Yaml][Inline] Fail properly on empty object tag and empty const tag
Browse files Browse the repository at this point in the history
  • Loading branch information
fancyweb committed Jan 14, 2020
1 parent db3134e commit 5396cbc
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/Symfony/Component/Yaml/Inline.php
Expand Up @@ -692,7 +692,11 @@ private static function evaluateScalar($scalar, $flags, $references = [])
return null;
case 0 === strpos($scalar, '!php/object'):
if (self::$objectSupport) {
return unserialize(self::parseScalar(substr($scalar, 12)));
if ((false === $str = substr($scalar, 12)) || '' === $str) {
throw new ParseException('The !php/object tag requires a value.', self::$parsedLineNumber + 1, $scalar, self::$parsedFilename);
}

return unserialize(self::parseScalar($str));
}

if (self::$exceptionOnInvalidType) {
Expand All @@ -717,8 +721,12 @@ private static function evaluateScalar($scalar, $flags, $references = [])
return null;
case 0 === strpos($scalar, '!php/const'):
if (self::$constantSupport) {
if ((false === $const = substr($scalar, 11)) || '' === $const) {
throw new ParseException('The !php/const tag requires a value.', self::$parsedLineNumber + 1, $scalar, self::$parsedFilename);
}

$i = 0;
if (\defined($const = self::parseScalar(substr($scalar, 11), 0, null, $i, false))) {
if (\defined($const = self::parseScalar($const, 0, null, $i, false))) {
return \constant($const);
}

Expand Down
27 changes: 27 additions & 0 deletions src/Symfony/Component/Yaml/Tests/InlineTest.php
Expand Up @@ -799,4 +799,31 @@ public function getTestsForOctalNumbers()
'negative octal number' => [-28, '-034'],
];
}

/**
* @dataProvider tagThrowsOnEmptyProvider
*/
public function testTagThrowsOnEmpty($tag, $inMapping, $flags)
{
$this->expectException(ParseException::class);
$this->expectExceptionMessage(sprintf('The %s tag requires a value at line 1 (near "%s").', $tag, $tag));

if ($inMapping) {
$value = sprintf('{%s : bar}', $tag);
} else {
$value = $tag.' ';
}

Inline::parse($value, $flags);
}

public function tagThrowsOnEmptyProvider()
{
return [
['!php/object', false, Yaml::PARSE_OBJECT],
['!php/object', true, Yaml::PARSE_OBJECT],
['!php/const', false, Yaml::PARSE_CONSTANT],
['!php/const', true, Yaml::PARSE_CONSTANT],
];
}
}

0 comments on commit 5396cbc

Please sign in to comment.