Skip to content

fix(contract): guard the OCC-21 repair slice against non-ASCII input#810

Merged
userFRM merged 2 commits into
mainfrom
fix/contract-fromstr-panic
Jun 16, 2026
Merged

fix(contract): guard the OCC-21 repair slice against non-ASCII input#810
userFRM merged 2 commits into
mainfrom
fix/contract-fromstr-panic

Conversation

@userFRM

@userFRM userFRM commented Jun 16, 2026

Copy link
Copy Markdown
Owner

What

Contract::from_str repairs a 20-byte OCC-21 identifier by peeling the trailing 15-char suffix and re-padding the root, slicing the input at byte index 5. The length guard tests the byte count (trimmed.len()), so a non-ASCII input could reach 20 bytes with the split landing inside a multi-byte codepoint. Slicing on a non-char-boundary panics, so "ABCDé260417C0055000".parse::<Contract>() aborted instead of returning an error.

Fix

OCC-21 identifiers are ASCII by spec (the strict 21-char parse_occ21 already enforces this before its own slices). Require trimmed.is_ascii() before the repair slices; non-ASCII input falls through to the bare-root validator, which returns a clean Contract::from_str error naming the offending input. Adds a regression test for the previously-panicking string.

Test

cargo test -p thetadatadx --lib fpss::protocol::contract — 45 pass including the new from_str_non_ascii_at_repair_length_does_not_panic.

🤖 Generated with Claude Code

Contract::from_str repairs a 20-byte OCC-21 string by slicing it at
byte index 5. The length check uses the byte count, so a non-ASCII
input could reach 20 bytes with the split landing inside a multi-byte
codepoint, panicking on a non-char-boundary slice. OCC-21 identifiers
are ASCII by spec, so require ASCII before the slice and let anything
else fall through to the bare-root validator, which returns a clean
error. Adds a regression test for the panicking input.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@userFRM userFRM enabled auto-merge (squash) June 16, 2026 09:15
A blank line before the explanatory comment stops rustfmt from
aligning it to the trailing-comment column of the preceding const.
@userFRM userFRM merged commit 687fffe into main Jun 16, 2026
43 checks passed
@userFRM userFRM deleted the fix/contract-fromstr-panic branch June 16, 2026 10:24
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.

2 participants