Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Yaml] deprecated non-escaped \ in double-quoted strings when parsing #16201

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
Oct 12, 2015
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
6 changes: 6 additions & 0 deletions src/Symfony/Component/Yaml/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
CHANGELOG
=========

2.8.0
-----

* Deprecated non-escaped \ in double-quoted strings when parsing Yaml
("Foo\Var" is not valid whereas "Foo\\Var" is)

2.1.0
-----

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,11 @@ php: |
array(
'double' => "some value\n \"some quoted string\" and 'some single quotes one'"
)
---
test: Backslashes
yaml: |
{ single: 'foo\Var', no-quotes: foo\Var, double: "foo\\Var" }
php: |
array(
'single' => 'foo\Var', 'no-quotes' => 'foo\Var', 'double' => 'foo\Var'
)
17 changes: 17 additions & 0 deletions src/Symfony/Component/Yaml/Tests/InlineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,23 @@ public function testHashStringsResemblingExponentialNumericsShouldNotBeChangedTo
$this->assertSame($value, Inline::parse(Inline::dump($value)));
}

/**
* @group legacy
* throws \Symfony\Component\Yaml\Exception\ParseException in 3.0
*/
public function testParseScalarWithNonEscapedBlackslashShouldThrowException()
{
$this->assertSame('Foo\Var', Inline::parse('"Foo\Var"'));
}

/**
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
*/
public function testParseScalarWithNonEscapedBlackslashAtTheEndShouldThrowException()
{
Inline::parse('"Foo\\"');
}

/**
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
*/
Expand Down
11 changes: 9 additions & 2 deletions src/Symfony/Component/Yaml/Unescaper.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Unescaper
/**
* Regex fragment that matches an escaped character in a double quoted string.
*/
const REGEX_ESCAPED_CHARACTER = "\\\\([0abt\tnvfre \\\"\\/\\\\N_LP]|x[0-9a-fA-F]{2}|u[0-9a-fA-F]{4}|U[0-9a-fA-F]{8})";
const REGEX_ESCAPED_CHARACTER = "\\\\(x[0-9a-fA-F]{2}|u[0-9a-fA-F]{4}|U[0-9a-fA-F]{8}|.)";

/**
* Unescapes a single quoted string.
Expand Down Expand Up @@ -70,10 +70,13 @@ public function unescapeDoubleQuotedString($value)
* @param string $value An escaped character
*
* @return string The unescaped character
*
* @internal This method is public to be usable as callback. It should not
* be used in user code. Should be changed in 3.0.
*/
public function unescapeCharacter($value)
{
switch ($value{1}) {
switch ($value[1]) {
Copy link
Member

Choose a reason for hiding this comment

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

not set when the unescaped backslash is the last character isn't it?

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually, a backslash at the end is not possible as this caught before this is called. I've added a test for this case and tweaked the regex here accordingly.

case '0':
return "\x0";
case 'a':
Expand Down Expand Up @@ -120,6 +123,10 @@ public function unescapeCharacter($value)
return self::utf8chr(hexdec(substr($value, 2, 4)));
case 'U':
return self::utf8chr(hexdec(substr($value, 2, 8)));
default:
@trigger_error('Not escaping a backslash in a double-quoted string is deprecated since Symfony 2.8 and will throw a ParseException in 3.0.', E_USER_DEPRECATED);

return $value;
}
}

Expand Down