From c9f5e53987f2e67c234546b576b15cce4bdfacbe Mon Sep 17 00:00:00 2001 From: Pavlo Kushnir Date: Mon, 8 Sep 2014 10:32:04 -0700 Subject: [PATCH] Fix bug in stripComments 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 | 11 +++++++---- folly/test/json_test_data/commented.json | 3 ++- folly/test/json_test_data/commented.json.exp | 3 ++- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/folly/json.cpp b/folly/json.cpp index f51b038b..d4f0771d 100644 --- a/folly/json.cpp +++ b/folly/json.cpp @@ -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; } diff --git a/folly/test/json_test_data/commented.json b/folly/test/json_test_data/commented.json index d718ef15..80632cb6 100644 --- a/folly/test/json_test_data/commented.json +++ b/folly/test/json_test_data/commented.json @@ -7,5 +7,6 @@ */ "test4": "foo /* bar", /* comment */ "te//": "foo", - "te/*": "bar" + "te/*": "bar", + "\\\"": "\\" /* comment */ } diff --git a/folly/test/json_test_data/commented.json.exp b/folly/test/json_test_data/commented.json.exp index 637e2c04..0c8482fd 100644 --- a/folly/test/json_test_data/commented.json.exp +++ b/folly/test/json_test_data/commented.json.exp @@ -2,5 +2,6 @@ "test": "foo", "test2": "foo // bar", "test4": "foo /* bar", "te//": "foo", - "te/*": "bar" + "te/*": "bar", + "\\\"": "\\" } -- 2.34.1