Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[Yaml] fixed string parsing (closes #4561)

  • Loading branch information...
commit 8272d98f8087878db37e8e5f6d4e37c5cc783dc3 1 parent fada541
@fabpot fabpot authored
Showing with 26 additions and 12 deletions.
  1. +15 −8 Inline.php
  2. +11 −4 Tests/InlineTest.php
View
23 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)));
}
View
15 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()
Please sign in to comment.
Something went wrong with that request. Please try again.