Fix bug in stripComments
authorPavlo Kushnir <pavlo@fb.com>
Mon, 8 Sep 2014 17:32:04 +0000 (10:32 -0700)
committerSara Golemon <sgolemon@fb.com>
Tue, 9 Sep 2014 21:22:23 +0000 (14:22 -0700)
Summary: the bug is in InString state when we escape only '"' character. We should escape at least '\\' and '"'. This diffs simply escapes any character after '\\'.

Test Plan: folly unit tests, mcrouter unit tests

Reviewed By: stepan@fb.com

Subscribers: njormrod

FB internal diff: D1540836

folly/json.cpp
folly/test/json_test_data/commented.json
folly/test/json_test_data/commented.json.exp

index f51b038b4f186891b6be8614bbc467eeded69a68..d4f0771d98a7d75c6138e0836462e3244ac100f4 100644 (file)
@@ -705,18 +705,21 @@ fbstring stripComments(StringPiece jsonC) {
           state = State::LineComment;
           ++i;
           continue;
-        } else if (s.startsWith("\"")) {
+        } else if (s[0] == '\"') {
           state = State::InString;
         }
         result.push_back(s[0]);
         break;
       case State::InString:
-        if (s.startsWith("\\\"")) {
+        if (s[0] == '\\') {
+          if (UNLIKELY(s.size() == 1)) {
+            throw std::logic_error("Invalid JSONC: string is not terminated");
+          }
           result.push_back(s[0]);
           result.push_back(s[1]);
           ++i;
           continue;
-        } else if (s.startsWith("\"")) {
+        } else if (s[0] == '\"') {
           state = State::None;
         }
         result.push_back(s[0]);
@@ -728,7 +731,7 @@ fbstring stripComments(StringPiece jsonC) {
         }
         break;
       case State::LineComment:
-        if (s.startsWith("\n")) {
+        if (s[0] == '\n') {
           // skip the line break. It doesn't matter.
           state = State::None;
         }
index d718ef15c4755db403ea9a8c6e66a64d7004ef69..80632cb64b21195b03d431b44970fb0a2bf94439 100644 (file)
@@ -7,5 +7,6 @@
   */
   "test4": "foo /* bar", /* comment */
   "te//": "foo",
-  "te/*": "bar"
+  "te/*": "bar",
+  "\\\"": "\\" /* comment */
 }
index 637e2c0409af76197a6aa7a4db4016935dc7c85d..0c8482fdd2ba2670d59bb46f90ea861373e3ca28 100644 (file)
@@ -2,5 +2,6 @@
     "test": "foo",   "test2": "foo // bar",   
   "test4": "foo /* bar", 
   "te//": "foo",
-  "te/*": "bar"
+  "te/*": "bar",
+  "\\\"": "\\" 
 }