Fix out-of-bounds access and identity comparison in FibonacciSearch - #7557
Merged
DenizAltunkapan merged 2 commits intoAug 5, 2026
Conversation
SEPURI-SAI-KRISHNA
requested review from
DenizAltunkapan,
alxkm and
yanglbme
as code owners
August 5, 2026 09:49
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #7557 +/- ##
============================================
+ Coverage 80.42% 80.43% +0.01%
- Complexity 7459 7462 +3
============================================
Files 815 815
Lines 24056 24056
Branches 4733 4733
============================================
+ Hits 19347 19350 +3
Misses 3945 3945
+ Partials 764 761 -3 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
DenizAltunkapan
approved these changes
Aug 5, 2026
DenizAltunkapan
left a comment
Member
There was a problem hiding this comment.
@SEPURI-SAI-KRISHNA thank you for your contribution
DenizAltunkapan
enabled auto-merge (squash)
August 5, 2026 19:37
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.
The final probe in
FibonacciSearch.findhas two defects:1. Out-of-bounds read. When the key is greater than every element, the main loop advances
offsetall the way ton - 1, soarray[offset + 1]reads past the end:The method is documented to return
-1when the key is absent. Randomised testing over sortedIntegerarrays of length 1–12 throws on roughly 6% of inputs.2. Reference equality instead of value comparison.
==compares object identity, not the values. It happens to work for smallIntegervalues because of theIntegercache (−128..127), which is why the existing tests pass, but it fails for anything else:Since
T extends Comparable<T>,compareTois the right comparison here and is consistent with the rest of the method.Fix
Tests
testFibonacciSearchKeyGreaterThanLastElement— a key above the maximum for lengths 1–50 must return-1, not throw.testFibonacciSearchFindsEqualButNotIdenticalKey— anIntegeroutside the cache and a non-internedString.testFibonacciSearchFindsEveryElement— every index of every array of length 1–50, with values above theIntegercache.All three fail on
masterand pass with the fix.clang-format -i --style=file path/to/your/file.java