fix: interpolation_search divide-by-zero panic and degenerate probe - #1062
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 pull request has been automatically marked as abandoned because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
|
Still relevant and ready for review, all 7 checks are green and Codecov reports every modified line covered. Recapping the impact for whoever picks this up: @imp2002 would you have a moment to take a look? |
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.