Skip to content

Commit 1f218e1

Browse files
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 <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]>
1 parent 64f68dc commit 1f218e1

File tree

4 files changed

+23
-1
lines changed

4 files changed

+23
-1
lines changed

include/nlohmann/detail/input/input_adapters.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <istream> // istream
2424
#endif // JSON_NO_IO
2525

26+
#include <nlohmann/detail/exceptions.hpp>
2627
#include <nlohmann/detail/iterators/iterator_traits.hpp>
2728
#include <nlohmann/detail/macro_scope.hpp>
2829
#include <nlohmann/detail/meta/type_traits.hpp>
@@ -420,6 +421,10 @@ typename container_input_adapter_factory_impl::container_input_adapter_factory<C
420421
// Special cases with fast paths
421422
inline file_input_adapter input_adapter(std::FILE* file)
422423
{
424+
if (file == nullptr)
425+
{
426+
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));
427+
}
423428
return file_input_adapter(file);
424429
}
425430

@@ -446,6 +451,10 @@ template < typename CharT,
446451
int >::type = 0 >
447452
contiguous_bytes_input_adapter input_adapter(CharT b)
448453
{
454+
if (b == nullptr)
455+
{
456+
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));
457+
}
449458
auto length = std::strlen(reinterpret_cast<const char*>(b));
450459
const auto* ptr = reinterpret_cast<const char*>(b);
451460
return input_adapter(ptr, ptr + length);

single_include/nlohmann/json.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6233,6 +6233,8 @@ NLOHMANN_JSON_NAMESPACE_END
62336233
#include <istream> // istream
62346234
#endif // JSON_NO_IO
62356235

6236+
// #include <nlohmann/detail/exceptions.hpp>
6237+
62366238
// #include <nlohmann/detail/iterators/iterator_traits.hpp>
62376239

62386240
// #include <nlohmann/detail/macro_scope.hpp>
@@ -6633,6 +6635,10 @@ typename container_input_adapter_factory_impl::container_input_adapter_factory<C
66336635
// Special cases with fast paths
66346636
inline file_input_adapter input_adapter(std::FILE* file)
66356637
{
6638+
if (file == nullptr)
6639+
{
6640+
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));
6641+
}
66366642
return file_input_adapter(file);
66376643
}
66386644

@@ -6659,6 +6665,10 @@ template < typename CharT,
66596665
int >::type = 0 >
66606666
contiguous_bytes_input_adapter input_adapter(CharT b)
66616667
{
6668+
if (b == nullptr)
6669+
{
6670+
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));
6671+
}
66626672
auto length = std::strlen(reinterpret_cast<const char*>(b));
66636673
const auto* ptr = reinterpret_cast<const char*>(b);
66646674
return input_adapter(ptr, ptr + length);

tests/src/unit-concepts.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ TEST_CASE("concepts")
2020
// a, b: values of type X: json
2121

2222
// TABLE 96 - Container Requirements
23-
2423
// X::value_type must return T
2524
CHECK((std::is_same<json::value_type, json>::value));
2625

tests/src/unit-deserialization.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,10 @@ TEST_CASE("deserialization")
361361
"start_object()", "key(one)", "number_unsigned(1)",
362362
"end_object()", "parse_error(29)"
363363
}));
364+
365+
const char* string = nullptr;
366+
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&);
367+
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&);
364368
}
365369

366370
SECTION("operator<<")

0 commit comments

Comments
 (0)