🐛 fixed undefined behavior bug
When an empty vector was passed to the parse function, an empty iterator range was used to construct an input iterator. Unfortunately, we then cannot use the start iterator to derive a pointer from. Found with Xcode's undefined behavior sanitizer.
This commit is contained in:
parent
8b123107c0
commit
07b78c993c
1 changed files with 11 additions and 2 deletions
13
src/json.hpp
13
src/json.hpp
|
@ -8886,8 +8886,17 @@ class basic_json
|
|||
static_assert(sizeof(typename std::iterator_traits<IteratorType>::value_type) == 1,
|
||||
"each element in the iterator range must have the size of 1 byte");
|
||||
|
||||
return create(reinterpret_cast<const char*>(&(*first)),
|
||||
static_cast<size_t>(std::distance(first, last)));
|
||||
const auto len = static_cast<size_t>(std::distance(first, last));
|
||||
if (JSON_LIKELY(len > 0))
|
||||
{
|
||||
// there is at least one element: use the address of first
|
||||
return create(reinterpret_cast<const char*>(&(*first)), len);
|
||||
}
|
||||
else
|
||||
{
|
||||
// the address of first cannot be used - use nullptr
|
||||
return create(nullptr, len);
|
||||
}
|
||||
}
|
||||
|
||||
/// input adapter for array
|
||||
|
|
Loading…
Reference in a new issue