From 1f218e1074a10494dbe0dccb83c5f3348cae8edf Mon Sep 17 00:00:00 2001 From: jh96 <36338184+jordan-hoang@users.noreply.github.com> Date: Tue, 19 Nov 2024 04:54:04 -0800 Subject: [PATCH] Possible fix for #4485 (#4487) * Possible fix for #4485 Throw's an exception when i is nullptr, also added a testcase for this scenario though most likely in the wrong test file.cpp * quick cleanup * Fix compile issues * moved tests around, changed exceptions, removed a possibly unneeded include * add back include for testing something * Ninja doesn't like not having a \n, at end of file, adding it back * update input_adapter file to deal with empty/null file ptr. * ran make pretty * added test for inputadapter * ran make amalgamate * Update tests/src/unit-deserialization.cpp Co-authored-by: Niels Lohmann * Update tests/src/unit-deserialization.cpp Co-authored-by: Niels Lohmann * Update input adapters.hpp with new includes * fix unabigious use of _, (there was a double declare) * did the amalagamate * rm duplicate includes * make amalgamate again * reorder * amalgamate * moved it above * amalgamate --------- Co-authored-by: Jordan Co-authored-by: Niels Lohmann --- include/nlohmann/detail/input/input_adapters.hpp | 9 +++++++++ single_include/nlohmann/json.hpp | 10 ++++++++++ tests/src/unit-concepts.cpp | 1 - tests/src/unit-deserialization.cpp | 4 ++++ 4 files changed, 23 insertions(+), 1 deletion(-) diff --git a/include/nlohmann/detail/input/input_adapters.hpp b/include/nlohmann/detail/input/input_adapters.hpp index 33fca3e4b9..58ad592098 100644 --- a/include/nlohmann/detail/input/input_adapters.hpp +++ b/include/nlohmann/detail/input/input_adapters.hpp @@ -23,6 +23,7 @@ #include // istream #endif // JSON_NO_IO +#include #include #include #include @@ -420,6 +421,10 @@ typename container_input_adapter_factory_impl::container_input_adapter_factory::type = 0 > contiguous_bytes_input_adapter input_adapter(CharT b) { + if (b == nullptr) + { + JSON_THROW(parse_error::create(101, 0, "attempting to parse an empty input; check that your input string or stream contains the expected JSON", nullptr)); + } auto length = std::strlen(reinterpret_cast(b)); const auto* ptr = reinterpret_cast(b); return input_adapter(ptr, ptr + length); diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 93d3a85016..8aaac1afb8 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -6233,6 +6233,8 @@ NLOHMANN_JSON_NAMESPACE_END #include // istream #endif // JSON_NO_IO +// #include + // #include // #include @@ -6633,6 +6635,10 @@ typename container_input_adapter_factory_impl::container_input_adapter_factory::type = 0 > contiguous_bytes_input_adapter input_adapter(CharT b) { + if (b == nullptr) + { + JSON_THROW(parse_error::create(101, 0, "attempting to parse an empty input; check that your input string or stream contains the expected JSON", nullptr)); + } auto length = std::strlen(reinterpret_cast(b)); const auto* ptr = reinterpret_cast(b); return input_adapter(ptr, ptr + length); diff --git a/tests/src/unit-concepts.cpp b/tests/src/unit-concepts.cpp index e042991845..950def3003 100644 --- a/tests/src/unit-concepts.cpp +++ b/tests/src/unit-concepts.cpp @@ -20,7 +20,6 @@ TEST_CASE("concepts") // a, b: values of type X: json // TABLE 96 - Container Requirements - // X::value_type must return T CHECK((std::is_same::value)); diff --git a/tests/src/unit-deserialization.cpp b/tests/src/unit-deserialization.cpp index c5735eda00..eaa5fe4e16 100644 --- a/tests/src/unit-deserialization.cpp +++ b/tests/src/unit-deserialization.cpp @@ -361,6 +361,10 @@ TEST_CASE("deserialization") "start_object()", "key(one)", "number_unsigned(1)", "end_object()", "parse_error(29)" })); + + const char* string = nullptr; + CHECK_THROWS_WITH_AS(_ = json::parse(string), "[json.exception.parse_error.101] parse error: attempting to parse an empty input; check that your input string or stream contains the expected JSON", json::parse_error&); + CHECK_THROWS_WITH_AS(_ = json::parse(nullptr), "[json.exception.parse_error.101] parse error: attempting to parse an empty input; check that your input string or stream contains the expected JSON", json::parse_error&); } SECTION("operator<<")