Skip to content

Stop binary writers overflowing the stack on deep values - #5518

Open
ayush-singh-0601 wants to merge 1 commit into
nlohmann:developfrom
ayush-singh-0601:binary-writer-stack-overflow
Open

ayush-singh-0601 wants to merge 1 commit into
nlohmann:developfrom
ayush-singh-0601:binary-writer-stack-overflow

Conversation

@ayush-singh-0601

Copy link
Copy Markdown

See #5392.

to_cbor, to_msgpack, and to_ubjson recurse once per nesting level. Parsing is iterative, so a value this library will happily build can still SIGSEGV when you serialize it.

This keeps the current recursive write for the first 128 levels (normal documents) and finishes anything deeper on a heap stack. Encoding is the same.

BSON is not in this PR. Its writer also walks the tree a second time to compute document lengths, so that belongs with the size-pass work.

  • 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.

to_cbor, to_msgpack, and to_ubjson recurse once per nesting level.
The parser is iterative, so a value the library accepts can crash on
the way back out.

Keep the existing recursive path for the first 128 levels and finish
anything deeper on a heap stack. Output is unchanged. BSON is left
alone because its extra size walk is a separate change.

See nlohmann#5392.

Signed-off-by: ayush-singh-0601 <singhayush062006@gmail.com>
@ayush-singh-0601
ayush-singh-0601 force-pushed the binary-writer-stack-overflow branch from ead14ec to 7eb828e Compare September 10, 2026 19:44
@nlohmann nlohmann added aspect: binary formats BSON, CBOR, MessagePack, UBJSON please rebase Please rebase your branch to origin/develop labels Sep 11, 2026

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

Please update from the develop branch.

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

Labels

aspect: binary formats BSON, CBOR, MessagePack, UBJSON L please rebase Please rebase your branch to origin/develop tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants