fix: copy_from_slice panic when k > number of indexed elements#40
Open
npiesco wants to merge 1 commit intorust-cv:mainfrom
Open
fix: copy_from_slice panic when k > number of indexed elements#40npiesco wants to merge 1 commit intorust-cv:mainfrom
npiesco wants to merge 1 commit intorust-cv:mainfrom
Conversation
search_layer used dest.copy_from_slice(&src[..found]) where found < dest.len(), causing a length mismatch panic. Fixed to dest[..found].copy_from_slice(&src[..found]).
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.
Bug
search_layerinhnsw_const.rspanics with acopy_from_slicelength mismatch when the requested number of neighbors (k/ef) exceeds the number of elements in the index.Root cause
Two sites in
search_layer(lines 309 and 321) use:copy_from_slicerequiressrc.len() == dest.len(). Whenfound < dest.len()(fewer elements in the index than requested), the source slice is shorter thandest→ panic.Fix
Slices
destto matchfound, which is what the next line (return &mut dest[..found]) already expects.MRE
See
tests/simple.rs::nearest_k_greater_than_index_size— inserts 8 vectors, searches withk=100:Before: panics with
copy_from_slice: source slice length (8) does not match destination slice length (100)After: returns 8 results
This surfaces whenever a caller passes
klarger than the index size, or when filtered searches reduce the effective candidate set belowk.