Skip to content

fix: prevent heap overflow in ByteLevel bulk path with overlapping sp… - #62

Merged
AlonKejzman merged 1 commit into
mainfrom
fix/byte-level-bulk-overflow-61
Aug 6, 2026
Merged

fix: prevent heap overflow in ByteLevel bulk path with overlapping sp…#62
AlonKejzman merged 1 commit into
mainfrom
fix/byte-level-bulk-overflow-61

Conversation

@AlonKejzman

Copy link
Copy Markdown
Collaborator

…lits

The bulk fast path (use_regex=false, add_prefix_space=false) reserved output capacity once from the input length, then encoded each split with an unchecked helper whose soundness assumed splits form a non-overlapping partition. Since PreTokenizedString::new accepts arbitrary caller-supplied splits, overlapping ranges make total encoded output exceed the reservation, so Vec::set_len runs past capacity — an out-of-bounds heap write reachable from safe code (CWE-787).

Use the capacity-checked encode_bytes_into (reserves per split) instead of encode_bytes_into_unchecked. The initial with_capacity still covers the common non-overlapping case, so reserve is a no-op there — no perf change — but overlapping splits can no longer overflow. Remove the now-unused unchecked helper and add a regression test.

Fixes #61

…lits

The bulk fast path (use_regex=false, add_prefix_space=false) reserved
output capacity once from the input length, then encoded each split with
an unchecked helper whose soundness assumed splits form a non-overlapping
partition. Since PreTokenizedString::new accepts arbitrary caller-supplied
splits, overlapping ranges make total encoded output exceed the
reservation, so Vec::set_len runs past capacity — an out-of-bounds heap
write reachable from safe code (CWE-787).

Use the capacity-checked encode_bytes_into (reserves per split) instead of
encode_bytes_into_unchecked. The initial with_capacity still covers the
common non-overlapping case, so reserve is a no-op there — no perf change —
but overlapping splits can no longer overflow. Remove the now-unused
unchecked helper and add a regression test.

Fixes #61

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@AlonKejzman
AlonKejzman merged commit a02e599 into main Aug 6, 2026
19 checks passed
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.

Heap buffer overflow in ByteLevel::pre_tokenize bulk path

1 participant