added zero-length array check, and better test coverage

This commit is contained in:
Francois Chabot 2019-01-22 17:15:59 -05:00
parent f76291cd3b
commit fb86e6da29
4 changed files with 47 additions and 2 deletions

View File

@ -399,7 +399,7 @@ class input_adapter
/// input adapter for array
template<class T, std::size_t N>
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<class ContiguousContainer, typename

View File

@ -2554,7 +2554,7 @@ class input_adapter
/// input adapter for array
template<class T, std::size_t N>
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<class ContiguousContainer, typename

View File

@ -1626,6 +1626,17 @@ TEST_CASE("parser class")
CHECK(j == json(true));
}
SECTION("from padded std::array")
{
std::array<uint8_t, 5> 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<U+0000>'; 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));

View File

@ -368,6 +368,19 @@ TEST_CASE("deserialization")
CHECK(l.events == std::vector<std::string>({"boolean(true)"}));
}
SECTION("from padded std::array")
{
std::array<uint8_t, 5> 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<std::string>({"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<std::string>({"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<std::string>({"parse_error(1)"}));
}
SECTION("from chars")
{
uint8_t* v = new uint8_t[5];