Skip to content

Fix out-of-bounds access and identity comparison in FibonacciSearch - #7557

Merged
DenizAltunkapan merged 2 commits into
TheAlgorithms:masterfrom
SEPURI-SAI-KRISHNA:fix/fibonacci-search-out-of-bounds
Aug 5, 2026
Merged

Fix out-of-bounds access and identity comparison in FibonacciSearch#7557
DenizAltunkapan merged 2 commits into
TheAlgorithms:masterfrom
SEPURI-SAI-KRISHNA:fix/fibonacci-search-out-of-bounds

Conversation

@SEPURI-SAI-KRISHNA

Copy link
Copy Markdown
Contributor

The final probe in FibonacciSearch.find has two defects:

if (fibMinus1 == 1 && array[offset + 1] == key) {
    return offset + 1;
}

1. Out-of-bounds read. When the key is greater than every element, the main loop advances offset all the way to n - 1, so array[offset + 1] reads past the end:

new FibonacciSearch().find(new Integer[] {10, 20, 30, 40}, 50);
// java.lang.ArrayIndexOutOfBoundsException: Index 4 out of bounds for length 4

The method is documented to return -1 when the key is absent. Randomised testing over sorted Integer arrays 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 small Integer values because of the Integer cache (−128..127), which is why the existing tests pass, but it fails for anything else:

new FibonacciSearch().find(new Integer[] {10, 20, 300}, 300);   // -1, expected 2
new FibonacciSearch().find(new String[] {"a", "b", "c"}, key);  // -1 for a non-interned "c"

Since T extends Comparable<T>, compareTo is the right comparison here and is consistent with the rest of the method.

Fix

if (fibMinus1 == 1 && offset + 1 < n && array[offset + 1].compareTo(key) == 0) {
    return offset + 1;
}

Tests

  • testFibonacciSearchKeyGreaterThanLastElement — a key above the maximum for lengths 1–50 must return -1, not throw.
  • testFibonacciSearchFindsEqualButNotIdenticalKey — an Integer outside the cache and a non-interned String.
  • testFibonacciSearchFindsEveryElement — every index of every array of length 1–50, with values above the Integer cache.

All three fail on master and pass with the fix.

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized it.
  • All filenames are in PascalCase.
  • All functions and variable names follow Java naming conventions.
  • All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
  • All new algorithms include a corresponding test class that validates their functionality.
  • All new code is formatted with clang-format -i --style=file path/to/your/file.java

@codecov-commenter

codecov-commenter commented Aug 5, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.43%. Comparing base (55f551b) to head (acf1090).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@DenizAltunkapan DenizAltunkapan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SEPURI-SAI-KRISHNA thank you for your contribution

@DenizAltunkapan
DenizAltunkapan enabled auto-merge (squash) August 5, 2026 19:37
@DenizAltunkapan
DenizAltunkapan merged commit 0b0f921 into TheAlgorithms:master Aug 5, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants