From 90d826043ddfb918e1f55bb044ee505058fb4c59 Mon Sep 17 00:00:00 2001 From: Zeeshan Date: Sun, 2 Aug 2026 04:08:13 +0200 Subject: [PATCH] fix: reject null input in BitonicSort with IllegalArgumentException Null arrays previously failed with an incidental NPE when reading length. Validate explicitly and add a regression test. Fixes #7549 --- src/main/java/com/thealgorithms/sorts/BitonicSort.java | 3 +++ .../java/com/thealgorithms/sorts/BitonicSortTest.java | 9 +++++++++ 2 files changed, 12 insertions(+) diff --git a/src/main/java/com/thealgorithms/sorts/BitonicSort.java b/src/main/java/com/thealgorithms/sorts/BitonicSort.java index 1c1a3ac45540..551c39d95a36 100644 --- a/src/main/java/com/thealgorithms/sorts/BitonicSort.java +++ b/src/main/java/com/thealgorithms/sorts/BitonicSort.java @@ -21,6 +21,9 @@ private enum Direction { */ @Override public > T[] sort(T[] array) { + if (array == null) { + throw new IllegalArgumentException("Array must not be null"); + } if (array.length == 0) { return array; } diff --git a/src/test/java/com/thealgorithms/sorts/BitonicSortTest.java b/src/test/java/com/thealgorithms/sorts/BitonicSortTest.java index 60c4bbe9d342..1fbaf174d211 100644 --- a/src/test/java/com/thealgorithms/sorts/BitonicSortTest.java +++ b/src/test/java/com/thealgorithms/sorts/BitonicSortTest.java @@ -1,8 +1,17 @@ package com.thealgorithms.sorts; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.junit.jupiter.api.Test; + public class BitonicSortTest extends SortingAlgorithmTest { @Override SortAlgorithm getSortAlgorithm() { return new BitonicSort(); } + + @Test + void testNullArrayThrowsIllegalArgumentException() { + assertThrows(IllegalArgumentException.class, () -> getSortAlgorithm().sort((Integer[]) null)); + } }