Repository navigation
fix: truncated_bytes() returns 0 on empty input (#2887) - #2889
Merged
Merged
Conversation
Stage 1 returns EMPTY on zero-length input before it writes the index sentinels that truncated_bytes() subtracts, so on a reused parser an empty stream reported the previous stream's value (possibly wrapped to 4294967295). Guard len == 0 in both the DOM and On-Demand streams; this also covers "[]" in comma_delimited_array mode and BOM-only input. Docs: state the empty-input behavior, list comma_delimited_array among the formats where the value is meaningless, turn the conditions into a list, and remove the duplicated paragraph in parse_many.md. Fixes #2887
This was referenced Sep 29, 2026
This was referenced Oct 3, 2026
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 #2887.
Bug
When the input is empty, stage 1 returns
EMPTYatjson_structural_indexer.h:197before it writes the index sentinels.truncated_bytes()subtracts those sentinels anyway, so on a reused parser an empty stream reports whatever the previous stream left behind. That can be a small stale count, or4294967295if the previous stream stopped mid-batch on an error. This matches the Arrow report ("only after repeated parser operations"). DOMparse_manyhas the same problem.Only zero-length input is affected: whitespace-only input still runs the final stage-1 pass, which writes the sentinels. I checked 1, 200 and 5000 bytes of whitespace, with batch sizes 64 and 1 MB.
Fix
if (len == 0) { return 0; }in bothtruncated_bytes()implementations. This also covers[]incomma_delimited_arraymode, BOM-only input, and a default-constructed On-Demand stream (null parser).truncated_bytes()is not on the parsing path, so throughput is unaffected.Docs
parse_many.mdhad the "only calltruncated_bytes()after iterating…" paragraph twice. The duplicate is removed.comma_delimited_arrayis now listed withjson_sequenceandcomma_delimitedas a format where the value is meaningless (it is rewritten tocomma_delimited). The header comments are updated to match.Tests
truncated_empty_stream_reused_parserin both the DOM and On-Demand suites. Each run first parses a stream with a truncated tail (expects 5), then an empty input in every format (expects 0). Without the fix, both tests fail withreturned '5' (expected '0').