8000 [Yaml] Only try last ressort parsing on multi lines · symfony/symfony@36474f8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 36474f8

Browse files
committed
[Yaml] Only try last ressort parsing on multi lines
1 parent 9e198cf commit 36474f8

File tree

2 files changed

+50
-7
lines changed

2 files changed

+50
-7
lines changed

src/Symfony/Component/Yaml/Parser.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ private function doParse($value, $flags)
402402
throw new ParseException('Multiple documents are not supported.', $this->currentLineNb + 1, $this->currentLine, $this->filename);
403403
}
404404

405-
if (isset($this->currentLine[1]) && '?' === $this->currentLine[0] && ' ' === $this->currentLine[1]) {
405+
if ($deprecatedUsage = (isset($this->currentLine[1]) && '?' === $this->currentLine[0] && ' ' === $this->currentLine[1])) {
406406
@trigger_error($this->getDeprecationMessage('Starting an unquoted string with a question mark followed by a space is deprecated since Symfony 3.3 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0.'), E_USER_DEPRECATED);
407407
}
408408

@@ -421,7 +421,16 @@ private function doParse($value, $flags)
421421
}
422422

423423
// try to parse the value as a multi-line string as a last resort
424-
if (0 === $this->currentLineNb && !$this->isNextLineIndented()) {
424+
if (0 === $this->currentLineNb && 1 < $this->totalNumberOfLines) {
425+
// If the indentation is not consistent at offset 0, it is to be considered as a ParseError
426+
if (0 === $this->offset && !$deprecatedUsage) {
427+
foreach ($this->lines as $line) {
428+
if (rtrim($line) !== trim($line)) {
429+
throw new ParseException('Unable to parse.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename);
430+
}
431+
}
432+
}
433+
425434
$previousLineWasNewline = false;
426435
$previousLineWasTerminatedWithBackslash = false;
427436
$value = '';

src/Symfony/Component/Yaml/Tests/ParserTest.php

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -812,15 +812,38 @@ public function testNonStringFollowedByCommentEmbeddedInMapping()
812812
$this->assertSame($expected, $this->parser->parse($yaml));
813813
}
814814

815+
public function getParseExceptionNotAffectedMultiLineStringLastResortParsing()
816+
{
817+
$tests = array();
818+
819+
$yaml = <<<'EOT'
820+
a
821+
b:
822+
EOT;
823+
$tests['parse error on first line'] = array($yaml);
824+
825+
$yaml = <<<'EOT'
826+
a
827+
828+
b
829+
c:
830+
EOT;
831+
$tests['parse error due to inconsistent indentation'] = array($yaml);
832+
833+
$yaml = <<<'EOT'
834+
& * ! | > ' " % @ ` #, { asd a;sdasd }-@^qw3
835+
EOT;
836+
$tests['symfony/symfony/issues/22967#issuecomment-322067742'] = array($yaml);
837+
838+
return $tests;
839+
}
840+
815841
/**
842+
* @dataProvider getParseExceptionNotAffectedMultiLineStringLastResortParsing
816843
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
817844
*/
818-
public function testMultiLineStringLastResortParsingOnlyAffectSameIndentationLevel()
845+
public function testParseExceptionNotAffectedByMultiLineStringLastResortParsing($yaml)
819846
{
820-
$yaml = <<<'EOT'
821-
parse
822-
error:
823-
EOT;
824847
$this->parser->parse($yaml);
825848
}
826849

@@ -837,6 +860,17 @@ public function testMultiLineStringLastResortParsing()
837860
);
838861

839862
$this->assertSame($expected, $this->parser->parse($yaml));
863+
864+
$yaml = <<<'EOT'
865+
a:
866+
b
867+
c
868+
EOT;
869+
$expected = array(
870+
'a' => 'b c',
871+
);
872+
873+
$this->assertSame($expected, $this->parser->parse($yaml));
840874
}
841875

842876
/**

0 commit comments

Comments
 (0)
0