Skip to content

Commit

Permalink
Possible fix for #4485 (#4487)
Browse files Browse the repository at this point in the history
* 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 <memory> 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 <[email protected]>

* Update tests/src/unit-deserialization.cpp

Co-authored-by: Niels Lohmann <[email protected]>

* 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 <[email protected]>
Co-authored-by: Niels Lohmann <[email protected]>
  • Loading branch information
3 people authored Nov 19, 2024
1 parent 64f68dc commit 1f218e1
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
9 changes: 9 additions & 0 deletions include/nlohmann/detail/input/input_adapters.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <istream> // istream
#endif // JSON_NO_IO

#include <nlohmann/detail/exceptions.hpp>
#include <nlohmann/detail/iterators/iterator_traits.hpp>
#include <nlohmann/detail/macro_scope.hpp>
#include <nlohmann/detail/meta/type_traits.hpp>
Expand Down Expand Up @@ -420,6 +421,10 @@ typename container_input_adapter_factory_impl::container_input_adapter_factory<C
// Special cases with fast paths
inline file_input_adapter input_adapter(std::FILE* file)
{
if (file == 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));
}
return file_input_adapter(file);
}

Expand All @@ -446,6 +451,10 @@ template < typename CharT,
int >::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<const char*>(b));
const auto* ptr = reinterpret_cast<const char*>(b);
return input_adapter(ptr, ptr + length);
Expand Down
10 changes: 10 additions & 0 deletions single_include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6233,6 +6233,8 @@ NLOHMANN_JSON_NAMESPACE_END
#include <istream> // istream
#endif // JSON_NO_IO

// #include <nlohmann/detail/exceptions.hpp>

// #include <nlohmann/detail/iterators/iterator_traits.hpp>

// #include <nlohmann/detail/macro_scope.hpp>
Expand Down Expand Up @@ -6633,6 +6635,10 @@ typename container_input_adapter_factory_impl::container_input_adapter_factory<C
// Special cases with fast paths
inline file_input_adapter input_adapter(std::FILE* file)
{
if (file == 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));
}
return file_input_adapter(file);
}

Expand All @@ -6659,6 +6665,10 @@ template < typename CharT,
int >::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<const char*>(b));
const auto* ptr = reinterpret_cast<const char*>(b);
return input_adapter(ptr, ptr + length);
Expand Down
1 change: 0 additions & 1 deletion tests/src/unit-concepts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<json::value_type, json>::value));

Expand Down
4 changes: 4 additions & 0 deletions tests/src/unit-deserialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<<")
Expand Down

0 comments on commit 1f218e1

Please sign in to comment.