🐛 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.
parent 8b123107
...@@ -8886,8 +8886,17 @@ class basic_json ...@@ -8886,8 +8886,17 @@ class basic_json
static_assert(sizeof(typename std::iterator_traits<IteratorType>::value_type) == 1, static_assert(sizeof(typename std::iterator_traits<IteratorType>::value_type) == 1,
"each element in the iterator range must have the size of 1 byte"); "each element in the iterator range must have the size of 1 byte");
return create(reinterpret_cast<const char*>(&(*first)), const auto len = static_cast<size_t>(std::distance(first, last));
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 /// input adapter for array
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment