From c680ec7e514bfd7f711f9681ef740ec95cdf67cd Mon Sep 17 00:00:00 2001 From: Jakub Zalas Date: Thu, 19 Jan 2012 00:01:56 +0000 Subject: [PATCH 1/2] Fixed json encoding when quoted value contained trailing backslash. Condition checking if current character is inside a quoted string did not consider the case when backslash before quote is escaped with another backslash. --- src/Composer/Json/JsonFile.php | 2 +- tests/Composer/Test/Json/JsonFileTest.php | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Composer/Json/JsonFile.php b/src/Composer/Json/JsonFile.php index 03c85250e..7201c80e0 100644 --- a/src/Composer/Json/JsonFile.php +++ b/src/Composer/Json/JsonFile.php @@ -130,7 +130,7 @@ class JsonFile $char = substr($json, $i, 1); // Are we inside a quoted string? - if ('"' === $char && '\\' !== $prevChar) { + if ('"' === $char && ('\\' !== $prevChar || '\\\\' == substr($json, $i-2, 2))) { $outOfQuotes = !$outOfQuotes; } elseif (':' === $char && $outOfQuotes) { // Add a space after the : character diff --git a/tests/Composer/Test/Json/JsonFileTest.php b/tests/Composer/Test/Json/JsonFileTest.php index 75e798388..231839416 100644 --- a/tests/Composer/Test/Json/JsonFileTest.php +++ b/tests/Composer/Test/Json/JsonFileTest.php @@ -93,6 +93,15 @@ class JsonFileTest extends \PHPUnit_Framework_TestCase $this->assertJsonFormat($json, $data); } + public function testTrailingBackslash() + { + $data = array('Metadata\\' => 'src/'); + $json = '{ + "Metadata\\\\": "src\/" +}'; + $this->assertJsonFormat($json, $data); + } + private function expectParseException($text, $json) { try { From dcdb761e1741de28774f9a38920e62aad8c45af1 Mon Sep 17 00:00:00 2001 From: Jakub Zalas Date: Thu, 19 Jan 2012 00:14:48 +0000 Subject: [PATCH 2/2] Used strict comparision. --- src/Composer/Json/JsonFile.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Composer/Json/JsonFile.php b/src/Composer/Json/JsonFile.php index 7201c80e0..fd43c9148 100644 --- a/src/Composer/Json/JsonFile.php +++ b/src/Composer/Json/JsonFile.php @@ -130,7 +130,7 @@ class JsonFile $char = substr($json, $i, 1); // Are we inside a quoted string? - if ('"' === $char && ('\\' !== $prevChar || '\\\\' == substr($json, $i-2, 2))) { + if ('"' === $char && ('\\' !== $prevChar || '\\\\' === substr($json, $i-2, 2))) { $outOfQuotes = !$outOfQuotes; } elseif (':' === $char && $outOfQuotes) { // Add a space after the : character