You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2024/02/23 23:32:47 UTC

(commons-lang) branch master updated: Make ArrayFill null-safe

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git


The following commit(s) were added to refs/heads/master by this push:
     new 8e05b7141 Make ArrayFill null-safe
8e05b7141 is described below

commit 8e05b7141a8fc302b35f412834d660f234a10648
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Fri Feb 23 18:32:45 2024 -0500

    Make ArrayFill null-safe
---
 src/changes/changes.xml                            |  1 +
 .../java/org/apache/commons/lang3/ArrayFill.java   | 48 ++++++++++------
 .../org/apache/commons/lang3/ArrayFillTest.java    | 64 ++++++++++++++++++++++
 3 files changed, 97 insertions(+), 16 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 8d65938f4..ebcb2fa20 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -108,6 +108,7 @@ The <action> type attribute can be add,update,fix,remove.
     <action                   type="fix" dev="ggregory" due-to="Gary Gregory">Deprecate SystemProperties 0-argument constructor.</action>
     <action                   type="fix" dev="ggregory" due-to="Gary Gregory">Deprecate ThreadUtils 0-argument constructor.</action>
     <action                   type="fix" dev="ggregory" due-to="Gary Gregory">Deprecate TypeUtils 0-argument constructor.</action>
+    <action                   type="fix" dev="ggregory" due-to="Gary Gregory">Make ArrayFill null-safe.</action>
     <!-- UPDATE -->
     <action                   type="update" dev="sebb" due-to="Dependabot">Bump commons-parent from 64 to 66.</action>
     <action                   type="update" dev="ggregory" due-to="Dependabot">Bump org.codehaus.mojo:exec-maven-plugin from 3.1.1 to 3.2.0 #1175.</action> 
diff --git a/src/main/java/org/apache/commons/lang3/ArrayFill.java b/src/main/java/org/apache/commons/lang3/ArrayFill.java
index a8de5e702..2efa1fc1d 100644
--- a/src/main/java/org/apache/commons/lang3/ArrayFill.java
+++ b/src/main/java/org/apache/commons/lang3/ArrayFill.java
@@ -29,91 +29,105 @@ public final class ArrayFill {
     /**
      * Fills and returns the given array.
      *
-     * @param a   the array to be filled.
+     * @param a   the array to be filled (may be null).
      * @param val the value to be stored in all elements of the array.
      * @return the given array.
      * @see Arrays#fill(byte[],byte)
      */
     public static byte[] fill(final byte[] a, final byte val) {
-        Arrays.fill(a, val);
+        if (a != null) {
+            Arrays.fill(a, val);
+        }
         return a;
     }
 
     /**
      * Fills and returns the given array.
      *
-     * @param a   the array to be filled.
+     * @param a   the array to be filled (may be null).
      * @param val the value to be stored in all elements of the array.
      * @return the given array.
      * @see Arrays#fill(char[],char)
      */
     public static char[] fill(final char[] a, final char val) {
-        Arrays.fill(a, val);
+        if (a != null) {
+            Arrays.fill(a, val);
+        }
         return a;
     }
 
     /**
      * Fills and returns the given array.
      *
-     * @param a   the array to be filled.
+     * @param a   the array to be filled (may be null).
      * @param val the value to be stored in all elements of the array.
      * @return the given array.
      * @see Arrays#fill(double[],double)
      */
     public static double[] fill(final double[] a, final double val) {
-        Arrays.fill(a, val);
+        if (a != null) {
+            Arrays.fill(a, val);
+        }
         return a;
     }
 
     /**
      * Fills and returns the given array.
      *
-     * @param a   the array to be filled.
+     * @param a   the array to be filled (may be null).
      * @param val the value to be stored in all elements of the array.
      * @return the given array.
      * @see Arrays#fill(float[],float)
      */
     public static float[] fill(final float[] a, final float val) {
-        Arrays.fill(a, val);
+        if (a != null) {
+            Arrays.fill(a, val);
+        }
         return a;
     }
 
     /**
      * Fills and returns the given array.
      *
-     * @param a   the array to be filled.
+     * @param a   the array to be filled (may be null).
      * @param val the value to be stored in all elements of the array.
      * @return the given array.
      * @see Arrays#fill(int[],int)
      */
     public static int[] fill(final int[] a, final int val) {
-        Arrays.fill(a, val);
+        if (a != null) {
+            Arrays.fill(a, val);
+        }
         return a;
     }
 
     /**
      * Fills and returns the given array.
      *
-     * @param a   the array to be filled.
+     * @param a   the array to be filled (may be null).
      * @param val the value to be stored in all elements of the array.
      * @return the given array.
      * @see Arrays#fill(long[],long)
      */
     public static long[] fill(final long[] a, final long val) {
-        Arrays.fill(a, val);
+        if (a != null) {
+            Arrays.fill(a, val);
+        }
         return a;
     }
 
     /**
      * Fills and returns the given array.
      *
-     * @param a   the array to be filled.
+     * @param a   the array to be filled (may be null).
      * @param val the value to be stored in all elements of the array.
      * @return the given array.
      * @see Arrays#fill(short[],short)
      */
     public static short[] fill(final short[] a, final short val) {
-        Arrays.fill(a, val);
+        if (a != null) {
+            Arrays.fill(a, val);
+        }
         return a;
     }
 
@@ -121,13 +135,15 @@ public final class ArrayFill {
      * Fills and returns the given array.
      *
      * @param <T> the array type.
-     * @param a   the array to be filled.
+     * @param a   the array to be filled (may be null).
      * @param val the value to be stored in all elements of the array.
      * @return the given array.
      * @see Arrays#fill(Object[],Object)
      */
     public static <T> T[] fill(final T[] a, final T val) {
-        Arrays.fill(a, val);
+        if (a != null) {
+            Arrays.fill(a, val);
+        }
         return a;
     }
 
diff --git a/src/test/java/org/apache/commons/lang3/ArrayFillTest.java b/src/test/java/org/apache/commons/lang3/ArrayFillTest.java
index 755ad5751..46f1fe77d 100644
--- a/src/test/java/org/apache/commons/lang3/ArrayFillTest.java
+++ b/src/test/java/org/apache/commons/lang3/ArrayFillTest.java
@@ -38,6 +38,14 @@ public class ArrayFillTest extends AbstractLangTest {
         }
     }
 
+    @Test
+    public void testFillByteArrayNull() {
+        final byte[] array = null;
+        final byte val = (byte) 1;
+        final byte[] actual = ArrayFill.fill(array, val);
+        assertSame(array, actual);
+    }
+
     @Test
     public void testFillCharArray() {
         final char[] array = new char[3];
@@ -49,6 +57,14 @@ public class ArrayFillTest extends AbstractLangTest {
         }
     }
 
+    @Test
+    public void testFillCharArrayNull() {
+        final char[] array = null;
+        final char val = 1;
+        final char[] actual = ArrayFill.fill(array, val);
+        assertSame(array, actual);
+    }
+
     @Test
     public void testFillDoubleArray() {
         final double[] array = new double[3];
@@ -60,6 +76,14 @@ public class ArrayFillTest extends AbstractLangTest {
         }
     }
 
+    @Test
+    public void testFillDoubleArrayNull() {
+        final double[] array = null;
+        final double val = 1;
+        final double[] actual = ArrayFill.fill(array, val);
+        assertSame(array, actual);
+    }
+
     @Test
     public void testFillFloatArray() {
         final float[] array = new float[3];
@@ -71,6 +95,14 @@ public class ArrayFillTest extends AbstractLangTest {
         }
     }
 
+    @Test
+    public void testFillFloatArrayNull() {
+        final float[] array = null;
+        final float val = 1;
+        final float[] actual = ArrayFill.fill(array, val);
+        assertSame(array, actual);
+    }
+
     @Test
     public void testFillIntArray() {
         final int[] array = new int[3];
@@ -82,6 +114,14 @@ public class ArrayFillTest extends AbstractLangTest {
         }
     }
 
+    @Test
+    public void testFillIntArrayNull() {
+        final int[] array = null;
+        final int val = 1;
+        final int[] actual = ArrayFill.fill(array, val);
+        assertSame(array, actual);
+    }
+
     @Test
     public void testFillLongArray() {
         final long[] array = new long[3];
@@ -93,6 +133,14 @@ public class ArrayFillTest extends AbstractLangTest {
         }
     }
 
+    @Test
+    public void testFillLongArrayNull() {
+        final long[] array = null;
+        final long val = 1;
+        final long[] actual = ArrayFill.fill(array, val);
+        assertSame(array, actual);
+    }
+
     @Test
     public void testFillObjectArray() {
         final String[] array = new String[3];
@@ -104,6 +152,14 @@ public class ArrayFillTest extends AbstractLangTest {
         }
     }
 
+    @Test
+    public void testFillObjectArrayNull() {
+        final Object[] array = null;
+        final Object val = 1;
+        final Object[] actual = ArrayFill.fill(array, val);
+        assertSame(array, actual);
+    }
+
     @Test
     public void testFillShortArray() {
         final short[] array = new short[3];
@@ -114,4 +170,12 @@ public class ArrayFillTest extends AbstractLangTest {
             assertEquals(val, v);
         }
     }
+
+    @Test
+    public void testFillShortArrayNull() {
+        final short[] array = null;
+        final short val = 1;
+        final short[] actual = ArrayFill.fill(array, val);
+        assertSame(array, actual);
+    }
 }