Repository navigation
Stop the BON8 writer overflowing the stack on deep values - #5806
Merged
nlohmann merged 3 commits intoOct 11, 2026
Merged
Conversation
to_bon8() recursed once per nesting level, so a value the iterative BON8 reader accepts (e.g. ~24k nested one-element arrays) crashed on the way back out. #5781 bounded the CBOR, MessagePack, and UBJSON/BJData writers, but BON8 was merged before it and was not covered. Apply the same scheme: recurse for the first recursion_depth_limit() levels, then finish the value with write_bon8_iterative, which keeps the open containers on a heap stack (reusing binary_container_frame) and writes the 0xFE closer when it leaves a container with more than four elements. The output is byte-for-byte unchanged. Fixes https://issues.oss-fuzz.com/issues/572238015 Signed-off-by: Niels Lohmann <mail@nlohmann.me>
gregmarr
reviewed
Oct 10, 2026
write_bon8_iterative and write_bon8_value_or_push handled arrays and objects in separate branches that repeated the end-of-container check, the 0xFE closer and the marker computation. Share those parts and branch only where arrays and objects really differ. The output is unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Hz7VJi1FTKr6gpseLErbbS Signed-off-by: Niels Lohmann <mail@nlohmann.me>
nlohmann
force-pushed
the
claude/json-binary-writer-stack-overflow-f33aa3
branch
from
October 10, 2026 21:17
60687a4 to
6964ce6
Compare
gregmarr
approved these changes
Oct 11, 2026
Signed-off-by: Niels Lohmann <mail@nlohmann.me> # Conflicts: # include/nlohmann/detail/output/binary_writer.hpp # single_include/nlohmann/json.hpp
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.
to_bon8()recursed once per nesting level. The BON8 reader does not recurse, so it accepts deeply nested input, for example about 24,000 nested one-element arrays. Writing that value back out then overflowed the stack. OSS-Fuzz 572238015 (parse_bon8_fuzzer) found this.#5781 already bounded the CBOR, MessagePack and UBJSON/BJData writers. BON8 (#2998) was merged before it and was not covered. This PR applies the same approach to BON8:
write_bon8_valuetakes adepth. Oncedepthreachesrecursion_depth_limit(), it passes the rest of the value towrite_bon8_iterative.write_bon8_iterativeandwrite_bon8_value_or_pushkeep the open containers on a heap stack, reusingbinary_container_frame. When they leave a container with more than four elements, they write the0xFEcloser.Tests in
unit-large_json.cpp: BON8 now joins the round-trip, meet-at-the-bound, byte-exact, deep-object and 100000-deep sections. New sections cover:0xFEclosers, adjacent strings) past the bound;The bug was in no release. It existed only on
developfrom #2998 (merged 2026-09-27) until this PR.Breaking changes to the public API
None. Only private members of
detail::binary_writerchanged.Written by Claude Code.
🤖 Generated with Claude Code