fix: interpolation_search divide-by-zero panic and degenerate probe - #1062
Open
SEPURI-SAI-KRISHNA wants to merge 1 commit into
Open
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1062 +/- ##
=======================================
Coverage 95.89% 95.90%
=======================================
Files 396 396
Lines 30440 30451 +11
=======================================
+ Hits 29190 29203 +13
+ Misses 1250 1248 -2 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
Pull Request Template
Description
interpolation_searchdivides bynums[high] - nums[low]without ever checking that thedifference is non-zero, and it performs that division before multiplying. Both are bugs.
1. Divide-by-zero panic
Whenever the two endpoints of the current range hold the same value, the probe computes
x / 0and the search aborts withattempt to divide by zero. This is not an exoticinput — it happens for:
interpolation_search(&[7], &7)interpolation_search(&[2, 2, 2], &2)interpolation_search(&[0, 1, 10], &10)Fuzzing the old implementation over 39,600 random (sorted slice, needle) pairs with slice
lengths 1..12 produced 2,044 panics (~5% of all searches). The five existing unit tests
missed it because each one happened to probe the correct index on the first iteration.
The fix keeps
low <= highas an explicit loop guard and returns early when the range isconstant — at that point the loop condition has already established
nums[low] <= item <= nums[high], so every element in the range is the item.2. The interpolation formula defeated its own purpose
The probe was written as:
Integer division ran first, so
(high - low) / (nums[high] - nums[low])truncated to0whenever the value range was wider than the index range — the common case. The offset then
collapsed to
low, and interpolation search degenerated into a linear scan, givingO(n)instead of the
O(log log n)the algorithm exists to provide. For example, searching9in[1, 2, 3, 4, 5, 6, 7, 8, 9, 100]stepped one index at a time.Reordering to multiply first restores the textbook formula:
3.
i32subtraction overflownums[high] - nums[low]and*item - nums[low]were computed ini32, so a slice spanninga wide range (e.g.
[i32::MIN, 0, i32::MAX]) overflowed. The intermediates are now widened,and the product is computed in
i128so it cannot overflow for any slice length.4. Unused generic parameter
The signature was
pub fn interpolation_search<Ordering>(nums: &[i32], item: &i32). Thatgeneric parameter is never used and shadows
std::cmp::Ordering, which forced every callsite into the nonsensical turbofish
interpolation_search::<Ordering>(&nums, &item). It hasbeen removed. This is the only breaking part of the change; nothing else in the repository
called this function.
The
Ok/Errcontract is unchanged:Ok(index)when found,Err(0)when absent.Tests
The five original assertions are preserved and extended to 25, including named regression
cases for each panic above, plus
matches_linear_scan_exhaustively, which checks the searchagainst a linear scan over ~33k generated cases (runs in well under 300ms).
Type of change
Checklist:
cargo clippy --all -- -D warningsjust before my last commit and fixed any issue that was found.cargo fmtjust before my last commit.cargo testjust before my last commit and all tests passed.mod.rsfile within its own folder, and in any parent folder(s).DIRECTORY.mdwith the correct link.COUNTRIBUTING.mdand my code follows its guidelines.