Skip to content

[Yaml] do not evaluate PHP constant names #25325

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Symfony/Component/Yaml/Inline.php
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,8 @@ private static function evaluateScalar($scalar, $flags, $references = array())
return;
case 0 === strpos($scalar, '!php/const'):
if (self::$constantSupport) {
if (defined($const = self::parseScalar(substr($scalar, 11)))) {
$i = 0;
if (defined($const = self::parseScalar(substr($scalar, 11), 0, null, $i, false))) {
return constant($const);
}

Expand Down
17 changes: 15 additions & 2 deletions src/Symfony/Component/Yaml/Tests/InlineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public function getTestsForParsePhpConstants()
array('!php/const PHP_INT_MAX', PHP_INT_MAX),
array('[!php/const PHP_INT_MAX]', array(PHP_INT_MAX)),
array('{ foo: !php/const PHP_INT_MAX }', array('foo' => PHP_INT_MAX)),
array('!php/const NULL', null),
);
}

Expand All @@ -82,10 +83,22 @@ public function testParsePhpConstantThrowsExceptionOnInvalidType()
/**
* @group legacy
* @expectedDeprecation The !php/const: tag to indicate dumped PHP constants is deprecated since version 3.4 and will be removed in 4.0. Use the !php/const (without the colon) tag instead on line 1.
* @dataProvider getTestsForParseLegacyPhpConstants
*/
public function testDeprecatedConstantTag()
public function testDeprecatedConstantTag($yaml, $expectedValue)
{
Inline::parse('!php/const:PHP_INT_MAX', Yaml::PARSE_CONSTANT);
$this->assertSame($expectedValue, Inline::parse($yaml, Yaml::PARSE_CONSTANT));
}

public function getTestsForParseLegacyPhpConstants()
{
return array(
array('!php/const:Symfony\Component\Yaml\Yaml::PARSE_CONSTANT', Yaml::PARSE_CONSTANT),
array('!php/const:PHP_INT_MAX', PHP_INT_MAX),
array('[!php/const:PHP_INT_MAX]', array(PHP_INT_MAX)),
array('{ foo: !php/const:PHP_INT_MAX }', array('foo' => PHP_INT_MAX)),
array('!php/const:NULL', null),
);
}

/**
Expand Down