Skip to content

Commit 0b0f921

Browse files
Fix out-of-bounds access and identity comparison in FibonacciSearch (#7557)
1 parent 55f551b commit 0b0f921

2 files changed

Lines changed: 50 additions & 1 deletion

File tree

src/main/java/com/thealgorithms/searches/FibonacciSearch.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public <T extends Comparable<T>> int find(T[] array, T key) {
6969
}
7070
}
7171

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

src/test/java/com/thealgorithms/searches/FibonacciSearchTest.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,53 @@ void testFibonacciSearchLargeArray() {
121121
int expectedIndex = 9999;
122122
assertEquals(expectedIndex, fibonacciSearch.find(array, key), "The index of the last element should be 9999.");
123123
}
124+
125+
/**
126+
* A key greater than every element used to throw {@link ArrayIndexOutOfBoundsException},
127+
* because the final probe read {@code array[offset + 1]} without checking the bound.
128+
*/
129+
@Test
130+
void testFibonacciSearchKeyGreaterThanLastElement() {
131+
FibonacciSearch fibonacciSearch = new FibonacciSearch();
132+
for (int length = 1; length <= 50; length++) {
133+
Integer[] array = new Integer[length];
134+
for (int i = 0; i < length; i++) {
135+
array[i] = i;
136+
}
137+
assertEquals(-1, fibonacciSearch.find(array, length), "A key above the maximum should not be found for length " + length + ".");
138+
}
139+
}
140+
141+
/**
142+
* The final probe used reference equality, so a key that is equal but not identical to the
143+
* stored element was reported as missing. Values above 127 are outside the {@link Integer}
144+
* cache and therefore are not the same object as the boxed array element.
145+
*/
146+
@Test
147+
void testFibonacciSearchFindsEqualButNotIdenticalKey() {
148+
FibonacciSearch fibonacciSearch = new FibonacciSearch();
149+
Integer[] array = {10, 20, 300};
150+
assertEquals(2, fibonacciSearch.find(array, Integer.valueOf(300)), "The index of the found element should be 2.");
151+
152+
String[] words = {"a", "b", "c"};
153+
String equalButDistinct = new StringBuilder("c").toString();
154+
assertEquals(2, fibonacciSearch.find(words, equalButDistinct), "The index of the found element should be 2.");
155+
}
156+
157+
/**
158+
* Every element must be found regardless of the array length.
159+
*/
160+
@Test
161+
void testFibonacciSearchFindsEveryElement() {
162+
FibonacciSearch fibonacciSearch = new FibonacciSearch();
163+
for (int length = 1; length <= 50; length++) {
164+
Integer[] array = new Integer[length];
165+
for (int i = 0; i < length; i++) {
166+
array[i] = 1000 + i * 2;
167+
}
168+
for (int i = 0; i < length; i++) {
169+
assertEquals(i, fibonacciSearch.find(array, Integer.valueOf(1000 + i * 2)), "Element at index " + i + " should be found for length " + length + ".");
170+
}
171+
}
172+
}
124173
}

0 commit comments

Comments
 (0)