[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
This commit is contained in:
Alex Cameron 2019-11-18 15:11:29 +01:00 committed by Raphael Isemann
parent 8c8c941844
commit 10b8514343
4 changed files with 104 additions and 3 deletions

View File

@ -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();

View File

@ -292,6 +292,8 @@ public:
JSONValue::SP ParseJSONValue();
protected:
JSONValue::SP ParseJSONValue(const std::string &value, const Token &token);
JSONValue::SP ParseJSONObject();
JSONValue::SP ParseJSONArray();

View File

@ -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

View File

@ -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);
});
}