Skip to content

Commit

Permalink
improve error when detecting unquoted asterisks
Browse files Browse the repository at this point in the history
Asterisks in unquoted strings are used in YAML to reference
variables. Before Symfony 2.3.19, Symfony 2.4.9 and Symfony 2.5.4,
unquoted asterisks in inlined YAML code were treated as regular
strings. This was fixed for the inline parser in #11677. However, an
unquoted * character now led to an error message like this:

```
PHP Warning:  array_key_exists(): The first argument should be either a string or an integer in vendor/symfony/symfony/src/Symfony/Component/Yaml/Inline.php on line 409

  [Symfony\Component\Yaml\Exception\ParseException]
  Reference "" does not exist at line 171 (near "- { foo: * }").
```
  • Loading branch information
xabbuh committed Sep 3, 2014
1 parent 1fc0575 commit 854e07b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Symfony/Component/Yaml/Inline.php
Expand Up @@ -392,7 +392,7 @@ private static function parseMapping($mapping, &$i = 0, $references = array())
*
* @return string A YAML string
*
* @throws ParseException when object parsing support was disabled and the parser detected a PHP object
* @throws ParseException when object parsing support was disabled and the parser detected a PHP object or when a reference could not be resolved
*/
private static function evaluateScalar($scalar, $references = array())
{
Expand All @@ -406,6 +406,11 @@ private static function evaluateScalar($scalar, $references = array())
$value = substr($scalar, 1);
}

// an unquoted *
if (false === $value || '' === $value) {
throw new ParseException('A reference must contain at least one character.');
}

if (!array_key_exists($value, $references)) {
throw new ParseException(sprintf('Reference "%s" does not exist.', $value));
}
Expand Down
18 changes: 18 additions & 0 deletions src/Symfony/Component/Yaml/Tests/InlineTest.php
Expand Up @@ -147,6 +147,24 @@ public function testParseMapReferenceInSequence()
$this->assertSame(array($foo), Inline::parse('[*foo]', false, false, array('foo' => $foo)));
}

/**
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
* @expectedExceptionMessage A reference must contain at least one character.
*/
public function testParseUnquotedAsterisk()
{
Inline::parse('{ foo: * }');
}

/**
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
* @expectedExceptionMessage A reference must contain at least one character.
*/
public function testParseUnquotedAsteriskFollowedByAComment()
{
Inline::parse('{ foo: * #foo }');
}

protected function getTestsForParse()
{
return array(
Expand Down

0 comments on commit 854e07b

Please sign in to comment.