From 3ab9a6eec5358a985e1b9c139a28b8965b364885 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Wed, 13 Jun 2012 14:57:32 +0200 Subject: [PATCH] [Yaml] fixed string parsing (closes #4561) --- src/Symfony/Component/Yaml/Inline.php | 23 ++++++++++++------- .../Component/Yaml/Tests/InlineTest.php | 15 ++++++++---- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/Symfony/Component/Yaml/Inline.php b/src/Symfony/Component/Yaml/Inline.php index 94818c61bfea..8bcfcda62b9a 100644 --- a/src/Symfony/Component/Yaml/Inline.php +++ b/src/Symfony/Component/Yaml/Inline.php @@ -21,8 +21,6 @@ class Inline { const REGEX_QUOTED_STRING = '(?:"([^"\\\\]*(?:\\\\.[^"\\\\]*)*)"|\'([^\']*(?:\'\'[^\']*)*)\')'; - const REGEX_SINGLE_QUOTED_STRING = '(?:\'([^\']*(?:\'\'[^\']*)*)\')(?!.*\')'; - const REGEX_DOUBLE_QUOTED_STRING = '(?:"([^"\\\\]*(?:\\\\.[^"\\\\]*)*)")(?!.*")'; /** * Converts a YAML string to a PHP array. @@ -52,7 +50,13 @@ static public function parse($value) $result = self::parseMapping($value); break; default: - $result = self::parseScalar($value); + $i = 0; + $result = self::parseScalar($value, null, array('"', "'"), $i); + + // some comment can end the scalar + if (preg_replace('/\s+#.*$/A', '', substr($value, $i))) { + throw new ParseException(sprintf('Unexpected characters near "%s".', substr($value, $i))); + } } if (isset($mbEncoding)) { @@ -163,6 +167,13 @@ static public function parseScalar($scalar, $delimiters = null, $stringDelimiter if (in_array($scalar[$i], $stringDelimiters)) { // quoted scalar $output = self::parseQuotedScalar($scalar, $i); + + if (null !== $delimiters) { + $tmp = ltrim(substr($scalar, $i), ' '); + if (!in_array($tmp[0], $delimiters)) { + throw new ParseException(sprintf('Unexpected characters (%s).', substr($scalar, $i))); + } + } } else { // "normal" string if (!$delimiters) { @@ -203,11 +214,7 @@ static private function parseQuotedScalar($scalar, &$i) $items = preg_split('/[\'"]\s*(?:[,:]|[}\]]\s*,)/', $subject); $subject = substr($subject, 0, strlen($items[0]) + 1); - if (($scalar[$i] == "'" - && !preg_match('/'.self::REGEX_SINGLE_QUOTED_STRING.'/Au', $subject, $match)) - || ($scalar[$i] == '"' - && !preg_match('/'.self::REGEX_DOUBLE_QUOTED_STRING.'/Au', $subject, $match)) - ) { + if (!preg_match('/'.self::REGEX_QUOTED_STRING.'/Au', substr($scalar, $i), $match)) { throw new ParseException(sprintf('Malformed inline YAML string (%s).', substr($scalar, $i))); } diff --git a/src/Symfony/Component/Yaml/Tests/InlineTest.php b/src/Symfony/Component/Yaml/Tests/InlineTest.php index 1714945dfc71..2684fa557eb8 100644 --- a/src/Symfony/Component/Yaml/Tests/InlineTest.php +++ b/src/Symfony/Component/Yaml/Tests/InlineTest.php @@ -66,23 +66,30 @@ public function testHashStringsResemblingExponentialNumericsShouldNotBeChangedTo } /** - * * @expectedException \Symfony\Component\Yaml\Exception\ParseException */ public function testParseScalarWithIncorrectlyQuotedStringShouldThrowException() { $value = "'don't do somthin' like that'"; - Inline::parseScalar($value); + Inline::parse($value); } /** - * * @expectedException \Symfony\Component\Yaml\Exception\ParseException */ public function testParseScalarWithIncorrectlyDoubleQuotedStringShouldThrowException() { $value = '"don"t do somthin" like that"'; - Inline::parseScalar($value); + Inline::parse($value); + } + + /** + * @expectedException \Symfony\Component\Yaml\Exception\ParseException + */ + public function testParseInvalidMappingKeyShouldThrowException() + { + $value = '{ "foo " bar": "bar" }'; + Inline::parse($value); } public function testParseScalarWithCorrectlyQuotedStringShouldReturnString()