From fb86e6da299405824cc6eda431726bbd8c22b346 Mon Sep 17 00:00:00 2001 From: Francois Chabot Date: Tue, 22 Jan 2019 17:15:59 -0500 Subject: [PATCH] added zero-length array check, and better test coverage --- .../nlohmann/detail/input/input_adapters.hpp | 2 +- single_include/nlohmann/json.hpp | 2 +- test/src/unit-class_parser.cpp | 20 +++++++++++++++ test/src/unit-deserialization.cpp | 25 +++++++++++++++++++ 4 files changed, 47 insertions(+), 2 deletions(-) diff --git a/include/nlohmann/detail/input/input_adapters.hpp b/include/nlohmann/detail/input/input_adapters.hpp index 949b8397c..490c05877 100644 --- a/include/nlohmann/detail/input/input_adapters.hpp +++ b/include/nlohmann/detail/input/input_adapters.hpp @@ -399,7 +399,7 @@ class input_adapter /// input adapter for array template input_adapter(T (&array)[N]) - : input_adapter(std::begin(array), array[N - 1] == 0 ? std::prev(std::end(array)) : std::end(array)) {} + : input_adapter(std::begin(array), (N > 0 && array[N - 1]) == 0 ? std::prev(std::end(array)) : std::end(array)) {} /// input adapter for contiguous container template input_adapter(T (&array)[N]) - : input_adapter(std::begin(array), array[N - 1] == 0 ? std::prev(std::end(array)) : std::end(array)) {} + : input_adapter(std::begin(array), (N > 0 && array[N - 1]) == 0 ? std::prev(std::end(array)) : std::end(array)) {} /// input adapter for contiguous container template v { {'t', 'r', 'u', 'e'} }; + json j; + json::parser(nlohmann::detail::input_adapter(std::begin(v), std::end(v))).parse(false, j); + CHECK(j == json(true)); + + CHECK_THROWS_WITH(json::parser(nlohmann::detail::input_adapter(std::begin(v), std::end(v))).parse(true, j), + "[json.exception.parse_error.101] parse error at line 1, column 5: syntax error while parsing value - invalid literal; last read: 'true'; expected end of input"); + } + SECTION("from array") { uint8_t v[] = {'t', 'r', 'u', 'e'}; @@ -1634,6 +1645,15 @@ TEST_CASE("parser class") CHECK(j == json(true)); } + SECTION("from zero-length array") + { + uint8_t v[] = {}; + json j; + CHECK_THROWS_WITH(json::parser(nlohmann::detail::input_adapter(v)).parse(true, j), + "[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - unexpected end of input; expected '[', '{', or a literal"); + } + + SECTION("from char literal") { CHECK(parser_helper("true") == json(true)); diff --git a/test/src/unit-deserialization.cpp b/test/src/unit-deserialization.cpp index 875147f8b..bf88e21d8 100644 --- a/test/src/unit-deserialization.cpp +++ b/test/src/unit-deserialization.cpp @@ -368,6 +368,19 @@ TEST_CASE("deserialization") CHECK(l.events == std::vector({"boolean(true)"})); } + SECTION("from padded std::array") + { + std::array v { {'t', 'r', 'u', 'e'} }; + + SaxEventLogger l; + CHECK(json::sax_parse(v, &l, json::input_format_t::json, false)); + CHECK(l.events.size() == 1); + CHECK(l.events == std::vector({"boolean(true)"})); + + CHECK(not json::accept(v)); + CHECK(not json::sax_parse(v, &l)); + } + SECTION("from array") { uint8_t v[] = {'t', 'r', 'u', 'e'}; @@ -380,6 +393,18 @@ TEST_CASE("deserialization") CHECK(l.events == std::vector({"boolean(true)"})); } + SECTION("from zero-length array") + { + uint8_t v[] = {}; + CHECK_THROWS_AS(json::parse(v), json::parse_error&); + CHECK(not json::accept(v)); + + SaxEventLogger l; + CHECK(not json::sax_parse(v, &l)); + CHECK(l.events.size() == 1); + CHECK(l.events == std::vector({"parse_error(1)"})); + } + SECTION("from chars") { uint8_t* v = new uint8_t[5];