Skip to content

Commit 61fe5f1

Browse files
committed
input_buffer_adapter: Fix handling of nullptr input
Clang UBSAN currently complains that the char * to input_buffer_adapter is a nullptr. Turns out it is actually required to accept nullptr, see for example line 415 in input_adapters.hpp ... // the address of first cannot be used: use nullptr ia = std::make_shared<input_buffer_adapter>(nullptr, len); .... Therefore we have to handle it gracefully here. We now also ignore the length parameter l if b is a nullptr.
1 parent 9ea3e19 commit 61fe5f1

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

include/nlohmann/detail/input/input_adapters.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,8 @@ class input_stream_adapter : public input_adapter_protocol
131131
class input_buffer_adapter : public input_adapter_protocol
132132
{
133133
public:
134-
JSON_HEDLEY_NON_NULL(2)
135134
input_buffer_adapter(const char* b, const std::size_t l) noexcept
136-
: cursor(b), limit(b + l)
135+
: cursor(b), limit(b == nullptr ? nullptr : (b + l))
137136
{}
138137

139138
// delete because of pointer members
@@ -147,6 +146,7 @@ class input_buffer_adapter : public input_adapter_protocol
147146
{
148147
if (JSON_HEDLEY_LIKELY(cursor < limit))
149148
{
149+
assert(cursor != nullptr and limit != nullptr);
150150
return std::char_traits<char>::to_int_type(*(cursor++));
151151
}
152152

single_include/nlohmann/json.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -3882,9 +3882,8 @@ class input_stream_adapter : public input_adapter_protocol
38823882
class input_buffer_adapter : public input_adapter_protocol
38833883
{
38843884
public:
3885-
JSON_HEDLEY_NON_NULL(2)
38863885
input_buffer_adapter(const char* b, const std::size_t l) noexcept
3887-
: cursor(b), limit(b + l)
3886+
: cursor(b), limit(b == nullptr ? nullptr : (b + l))
38883887
{}
38893888

38903889
// delete because of pointer members
@@ -3898,6 +3897,7 @@ class input_buffer_adapter : public input_adapter_protocol
38983897
{
38993898
if (JSON_HEDLEY_LIKELY(cursor < limit))
39003899
{
3900+
assert(cursor != nullptr and limit != nullptr);
39013901
return std::char_traits<char>::to_int_type(*(cursor++));
39023902
}
39033903

0 commit comments

Comments
 (0)