Skip to content

[Yaml] don't keep internal state between parser runs #22351

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 34 additions & 14 deletions src/Symfony/Component/Yaml/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,51 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
if (false === preg_match('//u', $value)) {
throw new ParseException('The YAML value does not appear to be valid UTF-8.');
}

$this->refs = array();

$mbEncoding = null;
$e = null;
$data = null;

if (function_exists('mb_internal_encoding') && ((int) ini_get('mbstring.func_overload')) & 2) {
$mbEncoding = mb_internal_encoding();
mb_internal_encoding('UTF-8');
}

try {
$data = $this->doParse($value, $exceptionOnInvalidType, $objectSupport, $objectForMap);
} catch (\Exception $e) {
} catch (\Throwable $e) {
}

if (null !== $mbEncoding) {
mb_internal_encoding($mbEncoding);
}

if (null !== $e) {
throw $e;
}

return $data;
}

private function doParse($value, $exceptionOnInvalidType = false, $objectSupport = false, $objectForMap = false)
{
$this->currentLineNb = -1;
$this->currentLine = '';
$value = $this->cleanup($value);
$this->lines = explode("\n", $value);
$this->locallySkippedLineNumbers = array();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these should also be reset at the end of the parsing to release memort


if (null === $this->totalNumberOfLines) {
$this->totalNumberOfLines = count($this->lines);
}

if (function_exists('mb_internal_encoding') && ((int) ini_get('mbstring.func_overload')) & 2) {
$mbEncoding = mb_internal_encoding();
mb_internal_encoding('UTF-8');
}

$data = array();
$context = null;
$allowOverwrite = false;

while ($this->moveToNextLine()) {
if ($this->isCurrentLineEmpty()) {
continue;
Expand Down Expand Up @@ -250,21 +278,13 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
throw $e;
}

if (isset($mbEncoding)) {
mb_internal_encoding($mbEncoding);
}

return $value;
}

throw new ParseException('Unable to parse.', $this->getRealCurrentLineNb() + 1, $this->currentLine);
}
}

if (isset($mbEncoding)) {
mb_internal_encoding($mbEncoding);
}

return empty($data) ? null : $data;
}

Expand All @@ -283,7 +303,7 @@ private function parseBlock($offset, $yaml, $exceptionOnInvalidType, $objectSupp
$parser = new self($offset, $this->totalNumberOfLines, $skippedLineNumbers);
$parser->refs = &$this->refs;

return $parser->parse($yaml, $exceptionOnInvalidType, $objectSupport, $objectForMap);
return $parser->doParse($yaml, $exceptionOnInvalidType, $objectSupport, $objectForMap);
}

/**
Expand Down
21 changes: 21 additions & 0 deletions src/Symfony/Component/Yaml/Tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1158,6 +1158,27 @@ public function testCanParseVeryLongValue()

$this->assertEquals($trickyVal, $arrayFromYaml);
}

/**
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
* @expectedExceptionMessage Reference "foo" does not exist at line 2
*/
public function testParserCleansUpReferencesBetweenRuns()
{
$yaml = <<<YAML
foo: &foo
baz: foobar
bar:
<<: *foo
YAML;
$this->parser->parse($yaml);

$yaml = <<<YAML
bar:
<<: *foo
YAML;
$this->parser->parse($yaml);
}
}

class B
Expand Down