Author: Alex Cameron Date: 2019-11-18T15:12:55+01:00 New Revision: 10b851434324f82e9ad11d39f501f2d8b921642a
URL: https://github.com/llvm/llvm-project/commit/10b851434324f82e9ad11d39f501f2d8b921642a DIFF: https://github.com/llvm/llvm-project/commit/10b851434324f82e9ad11d39f501f2d8b921642a.diff LOG: [lldb] Fix JSON parser to allow empty arrays Summary: Bugzilla: https://bugs.llvm.org/show_bug.cgi?id=39405 ``` alexc@kitty:~/work/wiredtiger/build_posix$ cat breakpoint.json [{"Breakpoint" : {"BKPTOptions" : {"AutoContinue" : false,"ConditionText" : "","EnabledState" : true,"IgnoreCount" : 0,"OneShotState" : false},"BKPTResolver" : {"Options" : {"NameMask" : [56],"Offset" : 0,"SkipPrologue" : true,"SymbolNames" : ["__wt_btcur_search"]},"Type" : "SymbolName"},"Hardware" : false,"SearchFilter" : {"Options" : {},"Type" : "Unconstrained","Foo" : []}}}] ``` **Before** ``` (lldb) breakpoint read --file breakpoint.json error: Invalid JSON from input file: /home/alexc/work/wiredtiger/build_posix/breakpoint.json. ``` **After** ``` (lldb) breakpoint read --file breakpoint.json New breakpoints: Breakpoint 1: where = libwiredtiger-3.2.2.so`__wt_btcur_search + 15 at bt_cursor.c:522:5, address = 0x00007ffff576ab2f ``` Reviewers: xbolva00, davide, labath Reviewed By: davide, labath Subscribers: mgorny, jingham, labath, davide, JDevlieghere, lldb-commits Tags: #llvm, #lldb Differential Revision: https://reviews.llvm.org/D68179 Added: lldb/unittests/debugserver/JSONTest.cpp Modified: lldb/tools/debugserver/source/JSON.cpp lldb/tools/debugserver/source/JSON.h lldb/unittests/debugserver/CMakeLists.txt Removed: ################################################################################ diff --git a/lldb/tools/debugserver/source/JSON.cpp b/lldb/tools/debugserver/source/JSON.cpp index 1b37767256d7..12d96d4ed4db 100644 --- a/lldb/tools/debugserver/source/JSON.cpp +++ b/lldb/tools/debugserver/source/JSON.cpp @@ -516,13 +516,16 @@ JSONValue::SP JSONParser::ParseJSONArray() { std::string value; std::string key; while (true) { - JSONValue::SP value_sp = ParseJSONValue(); + JSONParser::Token token = GetToken(value); + if (token == JSONParser::Token::ArrayEnd) + return JSONValue::SP(array_up.release()); + JSONValue::SP value_sp = ParseJSONValue(value, token); if (value_sp) array_up->AppendObject(value_sp); else break; - JSONParser::Token token = GetToken(value); + token = GetToken(value); if (token == JSONParser::Token::Comma) { continue; } else if (token == JSONParser::Token::ArrayEnd) { @@ -537,6 +540,11 @@ JSONValue::SP JSONParser::ParseJSONArray() { JSONValue::SP JSONParser::ParseJSONValue() { std::string value; const JSONParser::Token token = GetToken(value); + return ParseJSONValue(value, token); +} + +JSONValue::SP JSONParser::ParseJSONValue(const std::string &value, + const Token &token) { switch (token) { case JSONParser::Token::ObjectStart: return ParseJSONObject(); diff --git a/lldb/tools/debugserver/source/JSON.h b/lldb/tools/debugserver/source/JSON.h index b1c4a4c9db82..70bfdd7259ac 100644 --- a/lldb/tools/debugserver/source/JSON.h +++ b/lldb/tools/debugserver/source/JSON.h @@ -292,6 +292,8 @@ class JSONParser : public StdStringExtractor { JSONValue::SP ParseJSONValue(); protected: + JSONValue::SP ParseJSONValue(const std::string &value, const Token &token); + JSONValue::SP ParseJSONObject(); JSONValue::SP ParseJSONArray(); diff --git a/lldb/unittests/debugserver/CMakeLists.txt b/lldb/unittests/debugserver/CMakeLists.txt index adb8fb00ddac..a72b805c65b1 100644 --- a/lldb/unittests/debugserver/CMakeLists.txt +++ b/lldb/unittests/debugserver/CMakeLists.txt @@ -8,6 +8,7 @@ include_directories(${LLDB_SOURCE_DIR}/tools/debugserver/source ${LLDB_SOURCE_DIR}/tools/debugserver/source/MacOSX) add_lldb_unittest(debugserverTests + JSONTest.cpp RNBSocketTest.cpp debugserver_LogCallback.cpp @@ -24,8 +25,9 @@ if(IOS) WITH_FBS WITH_BKS ) - + add_lldb_unittest(debugserverNonUITests + JSONTest.cpp RNBSocketTest.cpp debugserver_LogCallback.cpp diff --git a/lldb/unittests/debugserver/JSONTest.cpp b/lldb/unittests/debugserver/JSONTest.cpp new file mode 100644 index 000000000000..54f6f96068ee --- /dev/null +++ b/lldb/unittests/debugserver/JSONTest.cpp @@ -0,0 +1,89 @@ +//===-- JSONTest.cpp --------------------------------------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "gmock/gmock.h" +#include "gtest/gtest.h" + +#include "JSON.h" + +template <typename T> +void TestJSON(JSONValue *json_val, const std::function<void(T &)> &test_func) { + ASSERT_THAT(json_val, testing::NotNull()); + ASSERT_TRUE(T::classof(json_val)); + test_func(static_cast<T &>(*json_val)); +} + +JSONValue::SP ParseJSON(const char *json_string) { + return JSONParser(json_string).ParseJSONValue(); +} + +template <typename T> +void ParseAndTestJSON( + const char *json_string, + const std::function<void(T &)> &test_func = [](T &) {}) { + auto json_val = ParseJSON(json_string); + TestJSON<T>(json_val.get(), test_func); +} + +TEST(JSON, Parse) { + ParseAndTestJSON<JSONString>("\"foo\"", [](JSONString &string_val) { + EXPECT_EQ(string_val.GetData(), "foo"); + }); + EXPECT_THAT(ParseJSON("\"foo"), testing::IsNull()); + ParseAndTestJSON<JSONNumber>("3", [](JSONNumber &number_val) { + EXPECT_EQ(number_val.GetAsSigned(), 3); + EXPECT_EQ(number_val.GetAsUnsigned(), 3u); + EXPECT_EQ(number_val.GetAsDouble(), 3.0); + }); + ParseAndTestJSON<JSONNumber>("-5", [](JSONNumber &number_val) { + EXPECT_EQ(number_val.GetAsSigned(), -5); + EXPECT_EQ(number_val.GetAsDouble(), -5.0); + }); + ParseAndTestJSON<JSONNumber>("-6.4", [](JSONNumber &number_val) { + EXPECT_EQ(number_val.GetAsSigned(), -6); + EXPECT_EQ(number_val.GetAsDouble(), -6.4); + }); + EXPECT_THAT(ParseJSON("-1.2.3"), testing::IsNull()); + ParseAndTestJSON<JSONTrue>("true"); + ParseAndTestJSON<JSONFalse>("false"); + ParseAndTestJSON<JSONNull>("null"); + ParseAndTestJSON<JSONObject>( + "{ \"key1\": 4, \"key2\": \"foobar\" }", [](JSONObject &obj_val) { + TestJSON<JSONNumber>(obj_val.GetObject("key1").get(), + [](JSONNumber &number_val) { + EXPECT_EQ(number_val.GetAsSigned(), 4); + EXPECT_EQ(number_val.GetAsUnsigned(), 4u); + EXPECT_EQ(number_val.GetAsDouble(), 4.0); + }); + TestJSON<JSONString>(obj_val.GetObject("key2").get(), + [](JSONString &string_val) { + EXPECT_EQ(string_val.GetData(), "foobar"); + }); + }); + ParseAndTestJSON<JSONArray>("[1, \"bar\", 3.14]", [](JSONArray &array_val) { + EXPECT_EQ(array_val.GetNumElements(), 3u); + TestJSON<JSONNumber>(array_val.GetObject(0).get(), + [](JSONNumber &number_val) { + EXPECT_EQ(number_val.GetAsSigned(), 1); + EXPECT_EQ(number_val.GetAsUnsigned(), 1u); + EXPECT_EQ(number_val.GetAsDouble(), 1.0); + }); + TestJSON<JSONString>( + array_val.GetObject(1).get(), + [](JSONString &string_val) { EXPECT_EQ(string_val.GetData(), "bar"); }); + TestJSON<JSONNumber>(array_val.GetObject(2).get(), + [](JSONNumber &number_val) { + EXPECT_EQ(number_val.GetAsSigned(), 3); + EXPECT_EQ(number_val.GetAsUnsigned(), 3u); + EXPECT_EQ(number_val.GetAsDouble(), 3.14); + }); + }); + ParseAndTestJSON<JSONArray>("[]", [](JSONArray &array_val) { + EXPECT_EQ(array_val.GetNumElements(), 0u); + }); +} _______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits