fix(decoder): reject negative array lengths in parse_header#59
Open
jamison wants to merge 1 commit into
Open
Conversation
Python's int() accepts negative integers without error, so a TOON document declaring a negative array length (e.g. `arr[-1]:`) would be silently parsed as a negative length. Downstream consumers then iterate with expected_length < 0, which either has no effect (empty loop) or produces a confusing strict-mode mismatch error rather than a clear parse failure. Add an explicit `length < 0` guard after the int() conversion so that negative lengths return None (treated as non-header by callers), matching the TypeScript reference implementation's behaviour after toon-format/toon#302. Note: a related bug — trailing delimiters producing a spurious empty final element in parse_delimited_values — also exists in this codebase and will be addressed in a follow-up issue once the expected behaviour for edge cases (empty input, lone delimiter) is confirmed against the spec fixtures. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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.
Summary
parse_header()usesint(length_str)to parse the bracket segment length, but has no guard against negative values.int("-5")succeeds in Python and returns-5, so a document declaringarr[-1]:would be silently accepted. Downstream consumers receive a negativeexpected_length, which either produces a silent no-op or a confusing strict-mode length-mismatch error rather than a clear parse failure at the header level.if length < 0: return Noneimmediately after theint()conversion, matching the behaviour of the TypeScript reference implementation (fixed in fix: encoder silent row drop, negative/non-decimal array lengths, trailing delimiter, colon re-search toon#302).Test results
817 passed, 13 skipped (all skips are pre-existing normalization roundtrip exclusions).
Related
A second bug exists in
parse_delimited_valuesin_parsing_utils.py: a trailing delimiter (e.g.a,b,) produces a spurious empty final element. The fix for that interacts with edge-case expectations for empty input (""→[]) and lone-delimiter input (","→["", ""]) that are not covered by the current spec fixtures. That will be tracked separately as an issue once the expected behaviour for those cases is confirmed against the spec.Companion fix to toon-format/toon#302 (TypeScript reference).
🤖 Generated with Claude Code