Skip to content

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

Merged
nlohmann merged 1 commit into
nlohmann:developfrom
xevrion:fix/bson-reader-unchecked-reads
Jul 30, 2026
Merged

check all BSON reads and add an EOF check for booleans#5332
nlohmann merged 1 commit into
nlohmann:developfrom
xevrion:fix/bson-reader-unchecked-reads

Conversation

@xevrion

@xevrion xevrion commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

Fixes #5309.

The BSON reader discarded the return values of three get_number() calls (the document size in parse_bson_internal and parse_bson_array, and the subtype in get_bson_binary) and read the boolean value with a bare get() 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 emits boolean(true) to the SAX handler before the element-list check reports the error, and a binary truncated at the subtype byte calls set_subtype() and get_binary() after its own parse error has already fired.

The two document-size reads now short-circuit with a plain if (!...) rather than JSON_HEDLEY_UNLIKELY, because that macro takes a single argument and the comma in get_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 the JSON_HEDLEY_UNLIKELY guard, and the boolean is routed through get_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.

  • The changes are described in detail, both the what and why.
  • If applicable, an existing issue is referenced.
  • The Code coverage remained at 100%. A test case for every new line of code.
  • If applicable, the documentation is updated.
  • The source code is amalgamated by running 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.

Signed-off-by: Yash Bavadiya <krbavadiya11@gmail.com>
@xevrion
xevrion force-pushed the fix/bson-reader-unchecked-reads branch from 5a7788d to 40fc7d9 Compare July 30, 2026 06:55

@nlohmann nlohmann left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

@nlohmann nlohmann added this to the Release 3.13.0 milestone Jul 30, 2026
@nlohmann
nlohmann merged commit dd24e2d into nlohmann:develop Jul 30, 2026
156 checks passed
@nlohmann

Copy link
Copy Markdown
Owner

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BSON reader ignores get_number() return values and skips an EOF check for booleans

2 participants