mirror of
https://github.com/nlohmann/json.git
synced 2026-06-07 05:09:44 +00:00
🐛 fix BSON conformance issue
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
@@ -172,6 +172,19 @@ class binary_reader
|
||||
std::int32_t document_size{};
|
||||
get_number<std::int32_t, true>(input_format_t::bson, document_size);
|
||||
|
||||
if (JSON_HEDLEY_UNLIKELY(document_size < 5))
|
||||
{
|
||||
auto last_token = get_token_string();
|
||||
return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
|
||||
exception_message(input_format_t::bson,
|
||||
concat("BSON document size must be at least 5, is ", std::to_string(document_size)),
|
||||
"document size"), nullptr));
|
||||
}
|
||||
|
||||
// chars_read now points just past the 4-byte size field;
|
||||
// the document started 4 bytes earlier and must end at start + document_size.
|
||||
const std::size_t expected_end = chars_read + static_cast<std::size_t>(document_size) - 4;
|
||||
|
||||
if (JSON_HEDLEY_UNLIKELY(!sax->start_object(detail::unknown_size())))
|
||||
{
|
||||
return false;
|
||||
@@ -182,6 +195,15 @@ class binary_reader
|
||||
return false;
|
||||
}
|
||||
|
||||
if (JSON_HEDLEY_UNLIKELY(chars_read != expected_end))
|
||||
{
|
||||
auto last_token = get_token_string();
|
||||
return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
|
||||
exception_message(input_format_t::bson,
|
||||
"BSON document terminator did not land at declared document size",
|
||||
"document"), nullptr));
|
||||
}
|
||||
|
||||
return sax->end_object();
|
||||
}
|
||||
|
||||
@@ -231,7 +253,21 @@ class binary_reader
|
||||
exception_message(input_format_t::bson, concat("string length must be at least 1, is ", std::to_string(len)), "string"), nullptr));
|
||||
}
|
||||
|
||||
return get_string(input_format_t::bson, len - static_cast<NumberType>(1), result) && get() != char_traits<char_type>::eof();
|
||||
if (JSON_HEDLEY_UNLIKELY(!get_string(input_format_t::bson, len - static_cast<NumberType>(1), result)))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (JSON_HEDLEY_UNLIKELY(get() != 0x00))
|
||||
{
|
||||
auto last_token = get_token_string();
|
||||
return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
|
||||
exception_message(input_format_t::bson,
|
||||
"BSON string is not null-terminated",
|
||||
"string"), nullptr));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/*!
|
||||
@@ -400,6 +436,19 @@ class binary_reader
|
||||
std::int32_t document_size{};
|
||||
get_number<std::int32_t, true>(input_format_t::bson, document_size);
|
||||
|
||||
if (JSON_HEDLEY_UNLIKELY(document_size < 5))
|
||||
{
|
||||
auto last_token = get_token_string();
|
||||
return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
|
||||
exception_message(input_format_t::bson,
|
||||
concat("BSON document size must be at least 5, is ", std::to_string(document_size)),
|
||||
"document size"), nullptr));
|
||||
}
|
||||
|
||||
// chars_read now points just past the 4-byte size field;
|
||||
// the document started 4 bytes earlier and must end at start + document_size.
|
||||
const std::size_t expected_end = chars_read + static_cast<std::size_t>(document_size) - 4;
|
||||
|
||||
if (JSON_HEDLEY_UNLIKELY(!sax->start_array(detail::unknown_size())))
|
||||
{
|
||||
return false;
|
||||
@@ -410,6 +459,15 @@ class binary_reader
|
||||
return false;
|
||||
}
|
||||
|
||||
if (JSON_HEDLEY_UNLIKELY(chars_read != expected_end))
|
||||
{
|
||||
auto last_token = get_token_string();
|
||||
return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
|
||||
exception_message(input_format_t::bson,
|
||||
"BSON document terminator did not land at declared document size",
|
||||
"document"), nullptr));
|
||||
}
|
||||
|
||||
return sax->end_array();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user