Skip to content

Stop the BON8 writer overflowing the stack on deep values - #5806

Merged
nlohmann merged 3 commits into
developfrom
claude/json-binary-writer-stack-overflow-f33aa3
Oct 11, 2026
Merged

nlohmann merged 3 commits into
developfrom
claude/json-binary-writer-stack-overflow-f33aa3

Conversation

@nlohmann

Copy link
Copy Markdown
Owner

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_value takes a depth. Once depth reaches recursion_depth_limit(), it passes the rest of the value to write_bon8_iterative.
  • write_bon8_iterative and write_bon8_value_or_push keep the open containers on a heap stack, reusing binary_container_frame. When they leave a container with more than four elements, they write the 0xFE closer.
  • The output does not change: byte-exact tests check depths around the bound.

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:

  • containers wider than four elements (0xFE closers, adjacent strings) past the bound;
  • discarded values past the bound (BON8 still writes nothing for them, as on the recursive path);
  • type_error.316 and out_of_range.407 thrown from below the bound;
  • the OSS-Fuzz regression itself.

The bug was in no release. It existed only on develop from #2998 (merged 2026-09-27) until this PR.

Breaking changes to the public API

None. Only private members of detail::binary_writer changed.

Written by Claude Code.

🤖 Generated with Claude Code

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>
@nlohmann nlohmann added the review needed It would be great if someone could review the proposed changes. label Oct 10, 2026
Comment thread include/nlohmann/detail/output/binary_writer.hpp Outdated
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
nlohmann force-pushed the claude/json-binary-writer-stack-overflow-f33aa3 branch from 60687a4 to 6964ce6 Compare October 10, 2026 21:17
@nlohmann nlohmann added 🚀 ready to merge Ready to merge - just waiting for CI to complete. and removed review needed It would be great if someone could review the proposed changes. labels Oct 11, 2026
@nlohmann nlohmann added this to the Release 3.13.0 milestone Oct 11, 2026
@github-actions github-actions Bot added aspect: binary formats BSON, CBOR, MessagePack, UBJSON L tests labels Oct 11, 2026
Signed-off-by: Niels Lohmann <mail@nlohmann.me>

# Conflicts:
#	include/nlohmann/detail/output/binary_writer.hpp
#	single_include/nlohmann/json.hpp
@nlohmann
nlohmann merged commit b38a2c9 into develop Oct 11, 2026
3 of 147 checks passed
@nlohmann
nlohmann deleted the claude/json-binary-writer-stack-overflow-f33aa3 branch October 11, 2026 06:24
nlohmann added a commit that referenced this pull request Oct 11, 2026
…st (#5807)

#5806 added the test while #5802 made to_bon8 reject discarded values; both
merged, so the test failed on develop.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
nlohmann added a commit that referenced this pull request Oct 11, 2026
#5795 and #5806 changed binary_writer.hpp but were merged without
updated amalgamated files, so the check_amalgamation run on develop
(c59ad95) fails. This is the output of 'make amalgamate'; no source
files change.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
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 🚀 ready to merge Ready to merge - just waiting for CI to complete. tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants