check all BSON reads and add an EOF check for booleans - #5332
Merged
nlohmann merged 1 commit intoJul 30, 2026
Conversation
Signed-off-by: Yash Bavadiya <krbavadiya11@gmail.com>
xevrion
force-pushed
the
fix/bson-reader-unchecked-reads
branch
from
July 30, 2026 06:55
5a7788d to
40fc7d9
Compare
Owner
|
Thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #5309.
The BSON reader discarded the return values of three
get_number()calls (the document size inparse_bson_internalandparse_bson_array, and the subtype inget_bson_binary) and read the boolean value with a bareget()that has no EOF check. As the issue notes, no truncated input escapes as an accepted document, but the failures surface downstream instead of at the failed read: a boolean truncated at EOF emitsboolean(true)to the SAX handler before the element-list check reports the error, and a binary truncated at the subtype byte callsset_subtype()andget_binary()after its own parse error has already fired.The two document-size reads now short-circuit with a plain
if (!...)rather thanJSON_HEDLEY_UNLIKELY, because that macro takes a single argument and the comma inget_number<std::int32_t, true>would be parsed as a macro argument separator; this matches the existing unwrapped call sites for the string, binary, and int32 element types. The subtype read keeps theJSON_HEDLEY_UNLIKELYguard, and the boolean is routed throughget_number<std::uint8_t>so it gets the same EOF handling as every other element type.Error messages for these truncated inputs now name the failed read (for example "BSON number" at byte 8 instead of "BSON element list" at byte 9). The existing "Incomplete BSON Input 2" assertion is unaffected because that input fails earlier, in
get_bson_cstring. Valid input parses identically.make amalgamate.Two new sections, "Incomplete BSON Input 5" and "Incomplete BSON Input 6", cover the boolean and subtype paths; the document-size branches are exercised by the existing "Incomplete BSON Input 4" and the empty-input cases. No documentation change is needed.