Avoid panic when byte_pair_encode is given an empty piece - #583
Open
devYRPauli wants to merge 1 commit into
Open
Avoid panic when byte_pair_encode is given an empty piece#583devYRPauli wants to merge 1 commit into
devYRPauli wants to merge 1 commit into
Conversation
byte_pair_encode special-cases a piece of length one but not length
zero. An empty slice falls through to the piece_len < 100 branch and
calls _byte_pair_merge, where `for i in 0..piece.len() - 1` underflows
usize and the following `piece[i..i + 2]` slice index panics.
This is reachable from Python: enc._encode_single_piece(b"") raises
PanicException("range end index 2 out of range for slice of length 0"),
even though enc.encode("") already returns an empty list.
Return an empty vector for an empty piece, matching that behaviour.
Co-Authored-By: Claude Opus 4.8 (1M context) <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.
byte_pair_encodespecial-cases a piece of length one but not length zero. An empty slice falls through to thepiece_len < 100branch into_byte_pair_merge, wherefor i in 0..piece.len() - 1underflowsusizeand the followingpiece[i..i + 2]slice index panics.This is reachable from Python:
encode("")already returns an empty list, so the panic is an inconsistency rather than intended behaviour.byte_pair_splithandles the same situation explicitly withassert!(piece.len() > 1).The fix returns an empty vector for an empty piece, before the existing length-one check.
Added a regression test alongside the existing ones in
src/lib.rs. Without the guard it panics atsrc/lib.rs:149; with it,cargo testpasses 3/3.Note: #559 proposed the same one-line guard. It was closed by its author without any maintainer response, so I do not believe the change was ever considered and rejected. Happy to close this if that is not the case.