Fix usize underflow in BPE / Strip decoders on empty / short input - #2245
Open
weixlu wants to merge 1 commit into
Open
Fix usize underflow in BPE / Strip decoders on empty / short input#2245weixlu wants to merge 1 commit into
weixlu wants to merge 1 commit into
Conversation
Sanjays2402
reviewed
Aug 2, 2026
|
|
||
| let mut stop_cut = chars.len(); | ||
| for i in 0..self.stop { | ||
| for i in 0..self.stop.min(chars.len()) { |
There was a problem hiding this comment.
the min() stops the underflow but the same short-token input still panics one line down: with Strip::new('H', 2, 1) and token "HH", start_cut ends at 2 and stop_cut at 1, so chars[start_cut..stop_cut] blows up with "slice index starts at 2 but ends at 1" (checked against the patched loop). needs a stop_cut.max(start_cut) before the slice.
Author
There was a problem hiding this comment.
Thanks for your review! Sure, I have fix the case you mentioned. Also I tested the following:
from tokenizers.decoders import Strip
print(repr(Strip(content='H', right=3).decode(['HH'])))
print(repr(Strip(content='H', left=2, right=1).decode(['HH'])))
print(repr(Strip(content='H', left=1).decode(['xHy'])))
print(repr(Strip(content='H', left=1, right=1).decode(['HHHH'])))Now it print '' '' 'xHy' 'HH', looks good
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.
Describe the Bug
decode_chain()of BPE / Strip decoders compute an index with an uncheckedusizesubtraction that may underflow.bpe.rs:tokens.len() - 1underflows when the token list is empty.strip.rs:chars.len() - i - 1underflows whenstopexceeds the token lengthReproduce
Run the code above, and
PanicExceptionwill be throw on debug build.Fix
The fix is quite straightforward, please take a look at the patch, it just contains 2 lines.
Notes
This issue was first reported by @devdan via fusil, the original report can be found here: https://github.com/devdanzin/fusil-extensions-findings/blob/main/tokenizers/reports/TOKENIZERS-0001-bpe-decoder-empty-underflow/report.md