Skip to content

fix(decoder): reject negative array lengths in parse_header#59

Open
jamison wants to merge 1 commit into
toon-format:mainfrom
jamison:fix/negative-length-and-trailing-delimiter
Open

fix(decoder): reject negative array lengths in parse_header#59
jamison wants to merge 1 commit into
toon-format:mainfrom
jamison:fix/negative-length-and-trailing-delimiter

Conversation

@jamison
Copy link
Copy Markdown

@jamison jamison commented May 13, 2026

Summary

  • parse_header() uses int(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 declaring arr[-1]: would be silently accepted. Downstream consumers receive a negative expected_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.
  • Fix: add if length < 0: return None immediately after the int() 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_values in _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

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>
@jamison jamison requested a review from a team as a code owner May 13, 2026 08:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant