From 96f609888b06a90b05071658e82ac3d733235c5a Mon Sep 17 00:00:00 2001 From: Daniel Hochman Date: Thu, 15 Jun 2017 14:32:12 -0700 Subject: [PATCH 1/2] throw when json is invalid --- source/common/json/json_loader.cc | 12 ++++++------ test/common/json/json_loader_test.cc | 16 ++++++++++++++++ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/source/common/json/json_loader.cc b/source/common/json/json_loader.cc index 5965b6c4990b..335716dc887e 100644 --- a/source/common/json/json_loader.cc +++ b/source/common/json/json_loader.cc @@ -531,7 +531,7 @@ bool ObjectHandler::StartObject() { state_ = expectKeyOrEndObject; return true; default: - NOT_REACHED; + return false; } } @@ -550,7 +550,7 @@ bool ObjectHandler::EndObject(rapidjson::SizeType) { } return true; default: - NOT_REACHED; + return false; } } @@ -561,7 +561,7 @@ bool ObjectHandler::Key(const char* value, rapidjson::SizeType size, bool) { state_ = expectValueOrStartObjectArray; return true; default: - NOT_REACHED; + return false; } } @@ -585,7 +585,7 @@ bool ObjectHandler::StartArray() { state_ = expectArrayValueOrEndArray; return true; default: - NOT_REACHED; + return false; } } @@ -605,7 +605,7 @@ bool ObjectHandler::EndArray(rapidjson::SizeType) { return true; default: - NOT_REACHED; + return false; } } @@ -650,7 +650,7 @@ bool ObjectHandler::handleValueEvent(FieldSharedPtr ptr) { stack_.top()->append(ptr); return true; default: - NOT_REACHED; + return false; } } diff --git a/test/common/json/json_loader_test.cc b/test/common/json/json_loader_test.cc index 57fd1c22056a..49f30fcccfa6 100644 --- a/test/common/json/json_loader_test.cc +++ b/test/common/json/json_loader_test.cc @@ -314,6 +314,22 @@ TEST(JsonLoaderTest, NestedSchema) { "key: #/value1"); } +TEST(JsonLoaderTest, MissingEnclosingDocument) { + + std::string json_string = R"EOF( + "listeners" : [ + { + "address": "tcp://127.0.0.1:1234", + "filters": [] + } + ] + )EOF"; + + EXPECT_THROW_WITH_MESSAGE(Factory::loadFromString(json_string), Exception, + "JSON supplied is not valid. Error(offset 14, line 2): Terminate " + "parsing due to Handler error.\n"); +} + TEST(JsonLoaderTest, AsString) { ObjectSharedPtr json = Factory::loadFromString("{\"name1\": \"value1\", \"name2\": true}"); json->iterate([&](const std::string& key, const Json::Object& value) { From f264f9f220999384f6e321034be0a3c5e581831c Mon Sep 17 00:00:00 2001 From: Daniel Hochman Date: Thu, 15 Jun 2017 14:53:52 -0700 Subject: [PATCH 2/2] only return false for known scenarios --- source/common/json/json_loader.cc | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/source/common/json/json_loader.cc b/source/common/json/json_loader.cc index 335716dc887e..dfdb0158d45c 100644 --- a/source/common/json/json_loader.cc +++ b/source/common/json/json_loader.cc @@ -531,7 +531,7 @@ bool ObjectHandler::StartObject() { state_ = expectKeyOrEndObject; return true; default: - return false; + NOT_REACHED; } } @@ -550,7 +550,7 @@ bool ObjectHandler::EndObject(rapidjson::SizeType) { } return true; default: - return false; + NOT_REACHED; } } @@ -561,7 +561,7 @@ bool ObjectHandler::Key(const char* value, rapidjson::SizeType size, bool) { state_ = expectValueOrStartObjectArray; return true; default: - return false; + NOT_REACHED; } } @@ -585,7 +585,7 @@ bool ObjectHandler::StartArray() { state_ = expectArrayValueOrEndArray; return true; default: - return false; + NOT_REACHED; } } @@ -605,7 +605,7 @@ bool ObjectHandler::EndArray(rapidjson::SizeType) { return true; default: - return false; + NOT_REACHED; } }