Skip to content

Commit 492efe5

Browse files
committed
fix(searches): add null and empty array safety to LowerBound and UpperBound
Guard against NullPointerException and empty array bounds access in LowerBound and UpperBound find methods when array is null/empty or key is null.
1 parent e7fbe44 commit 492efe5

2 files changed

Lines changed: 6 additions & 0 deletions

File tree

‎src/main/java/com/thealgorithms/searches/LowerBound.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ class LowerBound implements SearchAlgorithm {
3030
*/
3131
@Override
3232
public <T extends Comparable<T>> int find(T[] array, T key) {
33+
if (array == null || array.length == 0 || key == null) {
34+
return 0;
35+
}
3336
return search(array, key, 0, array.length - 1);
3437
}
3538

‎src/main/java/com/thealgorithms/searches/UpperBound.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ class UpperBound implements SearchAlgorithm {
3030
*/
3131
@Override
3232
public <T extends Comparable<T>> int find(T[] array, T key) {
33+
if (array == null || array.length == 0 || key == null) {
34+
return 0;
35+
}
3336
return search(array, key, 0, array.length - 1);
3437
}
3538

0 commit comments

Comments
 (0)