🐛 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:
Niels Lohmann 2017-07-07 23:37:16 +02:00
parent 8b123107c0
commit 07b78c993c
No known key found for this signature in database
GPG key ID: 7F3CEA63AE251B69

View file

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