Possible fix for #4485 (#4487)
Some checks are pending
Code scanning - action / CodeQL-Build (push) Waiting to run
macOS / macos-12 (13.1) (push) Waiting to run
macOS / macos-12 (13.2.1) (push) Waiting to run
macOS / macos-12 (13.3.1) (push) Waiting to run
macOS / macos-12 (13.4.1) (push) Waiting to run
macOS / macos-12 (14.0) (push) Waiting to run
macOS / macos-12 (14.0.1) (push) Waiting to run
macOS / macos-12 (14.1) (push) Waiting to run
macOS / macos-13 (14.2) (push) Waiting to run
macOS / macos-13 (14.3) (push) Waiting to run
macOS / macos-13 (14.3.1) (push) Waiting to run
macOS / macos-13 (15.0.1) (push) Waiting to run
macOS / macos-13 (15.1) (push) Waiting to run
macOS / macos-13 (15.2) (push) Waiting to run
macOS / macos-14 (15.3) (push) Waiting to run
macOS / macos-14 (15.4) (push) Waiting to run
macOS / macos-15 (16.0) (push) Waiting to run
macOS / macos-15 (16.1) (push) Waiting to run
macOS / xcode_standards (11) (push) Waiting to run
macOS / xcode_standards (14) (push) Waiting to run
macOS / xcode_standards (17) (push) Waiting to run
macOS / xcode_standards (20) (push) Waiting to run
macOS / xcode_standards (23) (push) Waiting to run
Ubuntu / ci_test_clang (push) Waiting to run
Ubuntu / ci_test_gcc (push) Waiting to run
Ubuntu / ci_static_analysis (ci_cppcheck) (push) Waiting to run
Ubuntu / ci_static_analysis (ci_infer) (push) Waiting to run
Ubuntu / ci_static_analysis (ci_single_binaries) (push) Waiting to run
Ubuntu / ci_static_analysis (ci_test_amalgamation) (push) Waiting to run
Ubuntu / ci_static_analysis (ci_test_single_header) (push) Waiting to run
Ubuntu / ci_static_analysis (ci_test_valgrind) (push) Waiting to run
Ubuntu / ci_static_analysis_ubuntu (ci_cpplint) (push) Waiting to run
Ubuntu / ci_static_analysis_ubuntu (ci_non_git_tests) (push) Waiting to run
Ubuntu / ci_static_analysis_ubuntu (ci_offline_testdata) (push) Waiting to run
Ubuntu / ci_static_analysis_ubuntu (ci_reproducible_tests) (push) Waiting to run
Ubuntu / ci_static_analysis_clang (ci_clang_analyze) (push) Waiting to run
Ubuntu / ci_static_analysis_clang (ci_clang_tidy) (push) Waiting to run
Ubuntu / ci_static_analysis_clang (ci_test_clang_sanitizer) (push) Waiting to run
Ubuntu / ci_cmake_options (ci_cmake_flags) (push) Waiting to run
Ubuntu / ci_cmake_options (ci_test_diagnostics) (push) Waiting to run
Ubuntu / ci_cmake_options (ci_test_legacycomparison) (push) Waiting to run
Ubuntu / ci_cmake_options (ci_test_noexceptions) (push) Waiting to run
Ubuntu / ci_cmake_options (ci_test_noglobaludls) (push) Waiting to run
Ubuntu / ci_cmake_options (ci_test_noimplicitconversions) (push) Waiting to run
Ubuntu / ci_test_coverage (push) Waiting to run
Ubuntu / ci_test_compilers_gcc_old (4.8) (push) Waiting to run
Ubuntu / ci_test_compilers_gcc_old (4.9) (push) Waiting to run
Ubuntu / ci_test_compilers_gcc_old (5) (push) Waiting to run
Ubuntu / ci_test_compilers_gcc_old (6) (push) Waiting to run
Ubuntu / ci_test_compilers_gcc (10) (push) Waiting to run
Ubuntu / ci_test_compilers_gcc (11) (push) Waiting to run
Ubuntu / ci_test_compilers_gcc (12) (push) Waiting to run
Ubuntu / ci_test_compilers_gcc (13) (push) Waiting to run
Ubuntu / ci_test_compilers_gcc (14) (push) Waiting to run
Ubuntu / ci_test_compilers_gcc (7) (push) Waiting to run
Ubuntu / ci_test_compilers_gcc (8) (push) Waiting to run
Ubuntu / ci_test_compilers_gcc (9) (push) Waiting to run
Ubuntu / ci_test_compilers_gcc (latest) (push) Waiting to run
Ubuntu / ci_test_compilers_clang (10) (push) Waiting to run
Ubuntu / ci_test_compilers_clang (11) (push) Waiting to run
Ubuntu / ci_test_compilers_clang (12) (push) Waiting to run
Ubuntu / ci_test_compilers_clang (13) (push) Waiting to run
Ubuntu / ci_test_compilers_clang (14) (push) Waiting to run
Ubuntu / ci_test_compilers_clang (15-bullseye) (push) Waiting to run
Ubuntu / ci_test_compilers_clang (16) (push) Waiting to run
Ubuntu / ci_test_compilers_clang (17) (push) Waiting to run
Ubuntu / ci_test_compilers_clang (18) (push) Waiting to run
Ubuntu / ci_test_compilers_clang (19) (push) Waiting to run
Ubuntu / ci_test_compilers_clang (3.5) (push) Waiting to run
Ubuntu / ci_test_compilers_clang (3.6) (push) Waiting to run
Ubuntu / ci_test_compilers_clang (3.7) (push) Waiting to run
Ubuntu / ci_test_compilers_clang (3.8) (push) Waiting to run
Ubuntu / ci_test_compilers_clang (3.9) (push) Waiting to run
Ubuntu / ci_test_compilers_clang (4) (push) Waiting to run
Ubuntu / ci_test_compilers_clang (5) (push) Waiting to run
Ubuntu / ci_test_compilers_clang (6) (push) Waiting to run
Ubuntu / ci_test_compilers_clang (7) (push) Waiting to run
Ubuntu / ci_test_compilers_clang (8) (push) Waiting to run
Ubuntu / ci_test_compilers_clang (9) (push) Waiting to run
Ubuntu / ci_test_compilers_clang (latest) (push) Waiting to run
Ubuntu / ci_test_compilers (g++-4.8) (push) Waiting to run
Ubuntu / ci_test_standards_gcc (11) (push) Waiting to run
Ubuntu / ci_test_standards_gcc (14) (push) Waiting to run
Ubuntu / ci_test_standards_gcc (17) (push) Waiting to run
Ubuntu / ci_test_standards_gcc (20) (push) Waiting to run
Ubuntu / ci_test_standards_gcc (23) (push) Waiting to run
Ubuntu / ci_test_standards_clang (11, libcxx) (push) Waiting to run
Ubuntu / ci_test_standards_clang (11, libstdcxx) (push) Waiting to run
Ubuntu / ci_test_standards_clang (14, libcxx) (push) Waiting to run
Ubuntu / ci_test_standards_clang (14, libstdcxx) (push) Waiting to run
Ubuntu / ci_test_standards_clang (17, libcxx) (push) Waiting to run
Ubuntu / ci_test_standards_clang (17, libstdcxx) (push) Waiting to run
Ubuntu / ci_test_standards_clang (20, libcxx) (push) Waiting to run
Ubuntu / ci_test_standards_clang (20, libstdcxx) (push) Waiting to run
Ubuntu / ci_test_standards_clang (23, libcxx) (push) Waiting to run
Ubuntu / ci_test_standards_clang (23, libstdcxx) (push) Waiting to run
Ubuntu / ci_cuda_example (push) Waiting to run
Ubuntu / ci_icpc (push) Waiting to run
Ubuntu / ci_reuse_compliance (push) Waiting to run
Ubuntu / ci_test_documentation (ci_test_api_documentation) (push) Waiting to run
Ubuntu / ci_test_documentation (ci_test_examples) (push) Waiting to run
Windows / mingw (x64) (push) Waiting to run
Windows / mingw (x86) (push) Waiting to run
Windows / msvc2019 (Win32, Debug) (push) Waiting to run
Windows / msvc2019 (Win32, Release) (push) Waiting to run
Windows / msvc2019 (x64, Debug) (push) Waiting to run
Windows / msvc2019 (x64, Release) (push) Waiting to run
Windows / msvc2019_latest (push) Waiting to run
Windows / msvc2022 (Win32, Debug) (push) Waiting to run
Windows / msvc2022 (Win32, Release) (push) Waiting to run
Windows / msvc2022 (x64, Debug) (push) Waiting to run
Windows / msvc2022 (x64, Release) (push) Waiting to run
Windows / msvc2022_latest (push) Waiting to run
Windows / clang (11) (push) Waiting to run
Windows / clang (12) (push) Waiting to run
Windows / clang (13) (push) Waiting to run
Windows / clang (14) (push) Waiting to run
Windows / clang (15) (push) Waiting to run
Windows / clang-cl-11 (Win32) (push) Waiting to run
Windows / clang-cl-11 (x64) (push) Waiting to run

* 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 <niels.lohmann@gmail.com>

* Update tests/src/unit-deserialization.cpp

Co-authored-by: Niels Lohmann <niels.lohmann@gmail.com>

* 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 <jordan-hoang@users.noreply.github.com>
Co-authored-by: Niels Lohmann <niels.lohmann@gmail.com>
This commit is contained in:
jh96 2024-11-19 04:54:04 -08:00 committed by GitHub
parent 64f68dc563
commit 1f218e1074
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 23 additions and 1 deletions

View File

@ -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>
@ -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);
}
@ -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);

View File

@ -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>
@ -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);
}
@ -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);

View File

@ -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));

View File

@ -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<<")