Skip to content

fix(md): Fix panic in write_prefix with short prefix#561

Merged
JeanMertz merged 2 commits intomainfrom
prr127
Apr 16, 2026
Merged

fix(md): Fix panic in write_prefix with short prefix#561
JeanMertz merged 2 commits intomainfrom
prr127

Conversation

@JeanMertz
Copy link
Copy Markdown
Collaborator

When self.prefix contained fewer than 2 bytes, the slice &self.prefix.as_bytes()[self.prefix.len() - 2..] would underflow, causing a panic at runtime.

Replace the bare subtraction with saturating_sub(2) so that a prefix of 0 or 1 bytes is handled gracefully — the window is seeded with whatever bytes are available rather than wrapping to usize::MAX.

A regression test for the 1-byte case and a sanity check for the empty prefix case are added in writer_tests.rs.

When `self.prefix` contained fewer than 2 bytes, the slice
`&self.prefix.as_bytes()[self.prefix.len() - 2..]` would underflow,
causing a panic at runtime.

Replace the bare subtraction with `saturating_sub(2)` so that a prefix
of 0 or 1 bytes is handled gracefully — the window is seeded with
whatever bytes are available rather than wrapping to `usize::MAX`.

A regression test for the 1-byte case and a sanity check for the empty
prefix case are added in `writer_tests.rs`.

Signed-off-by: Jean Mertz <git@jeanmertz.com>
Signed-off-by: Jean Mertz <git@jeanmertz.com>
@JeanMertz JeanMertz merged commit b70db56 into main Apr 16, 2026
13 checks passed
@JeanMertz JeanMertz deleted the prr127 branch April 16, 2026 09:04
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