Do not panic in the Strip decoder on crafted config or short tokens#2154
Open
v-code01 wants to merge 1 commit into
Open
Do not panic in the Strip decoder on crafted config or short tokens#2154v-code01 wants to merge 1 commit into
v-code01 wants to merge 1 commit into
Conversation
`Strip`'s `content`/`start`/`stop` fields are deserialized straight from the config with no validation, and `decode_chain` trusts them: - `let index = chars.len() - i - 1;` (i in `0..self.stop`) underflows when a token is shorter than `stop` — a panic in debug/overflow-checked builds, and an out-of-bounds index after wrapping in release. An empty token with `stop >= 1` triggers it. - `chars[start_cut..stop_cut]` panics on a reversed range when a token is made entirely of `content` characters, so `start_cut` overruns `stop_cut` (e.g. content `'H'`, start 2, stop 2, token `"HH"` → `chars[2..0]`). Bound the trailing loop by the token length and clamp `stop_cut` so the slice range is never reversed; both crafted cases now decode to an empty string. Adds a regression test.
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.
What
Strip'scontent/start/stopfields are deserialized straight from the config (tokenizer.json) with no validation, anddecode_chaintrusts them, so a crafted decoder config (plus tokens from the model vocab) panics:chars.len() - i - 1underflows for a token shorter thanstop: a panic in debug/overflow-checked builds, and an out-of-bounds index after wrapping in release. An empty token withstop >= 1triggers it ("attempt to subtract with overflow").contentcharacters,start_cutoverrunsstop_cut, sochars[start_cut..stop_cut]panics. E.g.Strip::new('H', 2, 2)decoding"HH"→chars[2..0]("slice index starts at 2 but ends at 0").Fix
Bound the trailing loop by the token length (
0..self.stop.min(chars.len())) and clampstop_cut = stop_cut.max(start_cut)so the slice range is never reversed. Both crafted cases now decode to an empty string, consistent with "everything was stripped".Tests
Adds
does_not_panic_on_crafted_configcovering the reversed-range and empty/short-token cases; the existingdecodetest is unchanged.cc @ArthurZucker @McPatate