Skip to content

Commit f47ced3

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

File tree

2 files changed

+49
-7
lines changed

2 files changed

+49
-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: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -812,15 +812,37 @@ public function testNonStringFollowedByCommentEmbeddedInMapping()
812812
$this->assertSame($expected, $this->parser->parse($yaml));
813813
}
814814

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

@@ -837,6 +859,17 @@ public function testMultiLineStringLastResortParsing()
837859
);
838860

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

842875
/**

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy