fix: prevent heap overflow in ByteLevel bulk path with overlapping sp… - #62
Merged
Conversation
…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>
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.
…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