fix: Whisper word timestamp OOB access on trailing replacement char#44902
Closed
guoyangzhen wants to merge 1 commit intohuggingface:mainfrom
Closed
fix: Whisper word timestamp OOB access on trailing replacement char#44902guoyangzhen wants to merge 1 commit intohuggingface:mainfrom
guoyangzhen wants to merge 1 commit intohuggingface:mainfrom
Conversation
Fixes IndexError in _split_tokens_on_unicode() when the decoded token stream ends with a dangling Unicode replacement character (U+FFFD). The computed index unicode_offset + decoded.index(replacement_char) can equal len(decoded_full), causing an out-of-bounds access. Fix: add bounds check before indexing into decoded_full. Fixes huggingface#44869
Contributor
|
[For maintainers] Suggested jobs to run (before merge) run-slow: whisper |
Member
|
No pure code agent PRs please, please check |
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.
Problem
_split_tokens_on_unicode()crashes withIndexError: string index out of rangewhen the decoded token stream ends with a dangling Unicode replacement character (\uFFFD).The computed index
unicode_offset + decoded.index(replacement_char)can equallen(decoded_full), causing an out-of-bounds string access at line ~1334:This happens when ASR output produces a run of valid tokens followed by a final incomplete Unicode fragment at EOF.
Root Cause
When processing tokens sequentially,
unicode_offsettracks the position indecoded_full. If the last decoded chunk ends with a replacement character andunicode_offset + decoded.index(replacement_char) == len(decoded_full), the index is one past the end of the string.Fix
Add a bounds check before indexing into
decoded_full:When
replacement_pos >= len(decoded_full), the word is treated as valid and split out (same as thereplacement_char not in decodedcase), which safely handles the trailing incomplete Unicode fragment.Testing
The fix handles the edge case where:
unicode_offset = 298decoded.index(replacement_char) = 0len(decoded_full) = 298target_index = 298(would cause OOB)With the bounds check, this correctly falls through to the word-splitting branch.
Fixes #44869