Skip to content

Commit

Permalink
input_buffer_adapter: Fix handling of nullptr input
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
t-b committed Sep 3, 2019
1 parent 9ea3e19 commit 61fe5f1
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions include/nlohmann/detail/input/input_adapters.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,8 @@ class input_stream_adapter : public input_adapter_protocol
class input_buffer_adapter : public input_adapter_protocol
{
public:
JSON_HEDLEY_NON_NULL(2)
input_buffer_adapter(const char* b, const std::size_t l) noexcept
: cursor(b), limit(b + l)
: cursor(b), limit(b == nullptr ? nullptr : (b + l))
{}

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

Expand Down
4 changes: 2 additions & 2 deletions single_include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3882,9 +3882,8 @@ class input_stream_adapter : public input_adapter_protocol
class input_buffer_adapter : public input_adapter_protocol
{
public:
JSON_HEDLEY_NON_NULL(2)
input_buffer_adapter(const char* b, const std::size_t l) noexcept
: cursor(b), limit(b + l)
: cursor(b), limit(b == nullptr ? nullptr : (b + l))
{}

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

Expand Down

0 comments on commit 61fe5f1

Please sign in to comment.