summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2020-08-29 20:00:36 +0100
committerDan Goodliffe <dan@randomdan.homeip.net>2020-08-29 20:00:36 +0100
commit9915229c03541128c9b715b4a274681026681d36 (patch)
tree6b31ca1d5975890fe097dfbb38cf435cf1480abc
parentClang-format and cppcheck (diff)
downloadlibjsonpp-9915229c03541128c9b715b4a274681026681d36.tar.bz2
libjsonpp-9915229c03541128c9b715b4a274681026681d36.tar.xz
libjsonpp-9915229c03541128c9b715b4a274681026681d36.zip
Fix handling of empty object and object with nothing after a comma
-rw-r--r--libjsonpp/json.ll8
-rw-r--r--libjsonpp/testParse.cpp48
2 files changed, 53 insertions, 3 deletions
diff --git a/libjsonpp/json.ll b/libjsonpp/json.ll
index 12e46a9..71f509f 100644
--- a/libjsonpp/json.ll
+++ b/libjsonpp/json.ll
@@ -33,6 +33,7 @@ escape "\\"
text [^\\\"]*
%x OBJECT_ITEM
+%x OBJECT_ITEM_OR_END
%x OBJECT_NEXT
%x ARRAY_ITEM
%x ARRAY_NEXT
@@ -63,13 +64,13 @@ text [^\\\"]*
yy_pop_state();
}
-<ARRAY_ITEM,INITIAL,OBJECT_ITEM>{beginstr} {
+<ARRAY_ITEM,INITIAL,OBJECT_ITEM,OBJECT_ITEM_OR_END>{beginstr} {
yy_push_state(STRING);
}
<ARRAY_ITEM,INITIAL>{beginobj} {
BeginObject();
- BEGIN(OBJECT_ITEM);
+ BEGIN(OBJECT_ITEM_OR_END);
}
<ARRAY_ITEM,INITIAL>{beginarray} {
@@ -87,6 +88,7 @@ text [^\\\"]*
yy_pop_state();
break;
case OBJECT_ITEM:
+ case OBJECT_ITEM_OR_END:
PushKey(encodeBuf());
BEGIN(COLON);
break;
@@ -94,7 +96,7 @@ text [^\\\"]*
buf.clear();
}
-<OBJECT_NEXT>{endobj} {
+<OBJECT_NEXT,OBJECT_ITEM_OR_END>{endobj} {
EndObject();
yy_pop_state();
}
diff --git a/libjsonpp/testParse.cpp b/libjsonpp/testParse.cpp
index f9eaf92..714cad4 100644
--- a/libjsonpp/testParse.cpp
+++ b/libjsonpp/testParse.cpp
@@ -78,6 +78,54 @@ BOOST_AUTO_TEST_CASE(parse_null)
std::get<json::Null>(json::parseValue(val));
}
+BOOST_AUTO_TEST_CASE(parse_empty_object)
+{
+ const Glib::ustring val(" { } ");
+ BOOST_REQUIRE(std::get<json::Object>(json::parseValue(val)).empty());
+}
+
+BOOST_AUTO_TEST_CASE(parse_broken_object_lone_number)
+{
+ const Glib::ustring val(" { 1 } ");
+ BOOST_REQUIRE_THROW(json::parseValue(val), json::ParseError);
+}
+
+BOOST_AUTO_TEST_CASE(parse_broken_object_lone_string)
+{
+ const Glib::ustring val(" { \"string\" } ");
+ BOOST_REQUIRE_THROW(json::parseValue(val), json::ParseError);
+}
+
+BOOST_AUTO_TEST_CASE(parse_broken_object_missing_value)
+{
+ const Glib::ustring val(" { \"string\": } ");
+ BOOST_REQUIRE_THROW(json::parseValue(val), json::ParseError);
+}
+
+BOOST_AUTO_TEST_CASE(parse_broken_object_missing_second_name)
+{
+ const Glib::ustring val(" { \"string\": 1, } ");
+ BOOST_REQUIRE_THROW(json::parseValue(val), json::ParseError);
+}
+
+BOOST_AUTO_TEST_CASE(parse_broken_object_missing_second_colon)
+{
+ const Glib::ustring val(" { \"string\": 1, \"string2\" } ");
+ BOOST_REQUIRE_THROW(json::parseValue(val), json::ParseError);
+}
+
+BOOST_AUTO_TEST_CASE(parse_broken_object_missing_second_value)
+{
+ const Glib::ustring val(" { \"string\": 1, \"string2\": } ");
+ BOOST_REQUIRE_THROW(json::parseValue(val), json::ParseError);
+}
+
+BOOST_AUTO_TEST_CASE(parse_broken_object_lone_bool)
+{
+ const Glib::ustring val(" { true } ");
+ BOOST_REQUIRE_THROW(json::parseValue(val), json::ParseError);
+}
+
BOOST_AUTO_TEST_CASE(parse_empty_array)
{
const Glib::ustring val(" [ ] ");