check all BSON reads and add an EOF check for booleans (#5332)

Signed-off-by: Yash Bavadiya <krbavadiya11@gmail.com>
This commit is contained in:
Yash Bavadiya
2026-07-30 23:44:31 +02:00
committed by GitHub
parent 868506dcc0
commit dd24e2dffd
3 changed files with 63 additions and 8 deletions
@@ -197,7 +197,10 @@ class binary_reader
{
const std::size_t document_start = chars_read;
std::int32_t document_size{};
get_number<std::int32_t, true>(input_format_t::bson, document_size);
if (!get_number<std::int32_t, true>(input_format_t::bson, document_size))
{
return false;
}
if (JSON_HEDLEY_UNLIKELY(!sax->start_object(detail::unknown_size())))
{
@@ -287,7 +290,10 @@ class binary_reader
// All BSON binary values have a subtype
std::uint8_t subtype{};
get_number<std::uint8_t>(input_format_t::bson, subtype);
if (JSON_HEDLEY_UNLIKELY(!get_number<std::uint8_t>(input_format_t::bson, subtype)))
{
return false;
}
result.set_subtype(subtype);
return get_binary(input_format_t::bson, len, result);
@@ -340,7 +346,8 @@ class binary_reader
case 0x08: // boolean
{
return sax->boolean(get() != 0);
std::uint8_t value{};
return get_number<std::uint8_t>(input_format_t::bson, value) && sax->boolean(value != 0);
}
case 0x0A: // null
@@ -431,7 +438,10 @@ class binary_reader
{
const std::size_t document_start = chars_read;
std::int32_t document_size{};
get_number<std::int32_t, true>(input_format_t::bson, document_size);
if (!get_number<std::int32_t, true>(input_format_t::bson, document_size))
{
return false;
}
if (JSON_HEDLEY_UNLIKELY(!sax->start_array(detail::unknown_size())))
{
+14 -4
View File
@@ -10746,7 +10746,10 @@ class binary_reader
{
const std::size_t document_start = chars_read;
std::int32_t document_size{};
get_number<std::int32_t, true>(input_format_t::bson, document_size);
if (!get_number<std::int32_t, true>(input_format_t::bson, document_size))
{
return false;
}
if (JSON_HEDLEY_UNLIKELY(!sax->start_object(detail::unknown_size())))
{
@@ -10836,7 +10839,10 @@ class binary_reader
// All BSON binary values have a subtype
std::uint8_t subtype{};
get_number<std::uint8_t>(input_format_t::bson, subtype);
if (JSON_HEDLEY_UNLIKELY(!get_number<std::uint8_t>(input_format_t::bson, subtype)))
{
return false;
}
result.set_subtype(subtype);
return get_binary(input_format_t::bson, len, result);
@@ -10889,7 +10895,8 @@ class binary_reader
case 0x08: // boolean
{
return sax->boolean(get() != 0);
std::uint8_t value{};
return get_number<std::uint8_t>(input_format_t::bson, value) && sax->boolean(value != 0);
}
case 0x0A: // null
@@ -10980,7 +10987,10 @@ class binary_reader
{
const std::size_t document_start = chars_read;
std::int32_t document_size{};
get_number<std::int32_t, true>(input_format_t::bson, document_size);
if (!get_number<std::int32_t, true>(input_format_t::bson, document_size))
{
return false;
}
if (JSON_HEDLEY_UNLIKELY(!sax->start_array(detail::unknown_size())))
{
+35
View File
@@ -825,6 +825,41 @@ TEST_CASE("Incomplete BSON Input")
CHECK(!json::sax_parse(incomplete_bson, &scp, json::input_format_t::bson));
}
SECTION("Incomplete BSON Input 5")
{
std::vector<std::uint8_t> const incomplete_bson =
{
0x09, 0x00, 0x00, 0x00, // size (little endian)
0x08, // entry: boolean
'b', '\x00' // key, unexpected EOF before the value
};
json _;
CHECK_THROWS_WITH_AS(_ = json::from_bson(incomplete_bson), "[json.exception.parse_error.110] parse error at byte 8: syntax error while parsing BSON number: unexpected end of input", json::parse_error&);
CHECK(json::from_bson(incomplete_bson, true, false).is_discarded());
SaxCountdown scp(0);
CHECK(!json::sax_parse(incomplete_bson, &scp, json::input_format_t::bson));
}
SECTION("Incomplete BSON Input 6")
{
std::vector<std::uint8_t> const incomplete_bson =
{
0x0F, 0x00, 0x00, 0x00, // size (little endian)
0x05, // entry: binary
'b', '\x00', // key
0x00, 0x00, 0x00, 0x00 // length, unexpected EOF before the subtype
};
json _;
CHECK_THROWS_WITH_AS(_ = json::from_bson(incomplete_bson), "[json.exception.parse_error.110] parse error at byte 12: syntax error while parsing BSON number: unexpected end of input", json::parse_error&);
CHECK(json::from_bson(incomplete_bson, true, false).is_discarded());
SaxCountdown scp(0);
CHECK(!json::sax_parse(incomplete_bson, &scp, json::input_format_t::bson));
}
SECTION("Improve coverage")
{
SECTION("key")