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/05/18 11:52:32 UTC

(commons-compress) branch master updated: Fix SpotBugs NP_NULL_PARAM_DEREF in Sets.newHashSet()

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-compress.git


The following commit(s) were added to refs/heads/master by this push:
     new a52684345 Fix SpotBugs NP_NULL_PARAM_DEREF in Sets.newHashSet()
     new 59f3af555 Merge branch 'master' of https://gitbox.apache.org/repos/asf/commons-compress.git
a52684345 is described below

commit a5268434553233d57450b0d9663f57e88c6b9537
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat May 18 07:51:55 2024 -0400

    Fix SpotBugs NP_NULL_PARAM_DEREF in Sets.newHashSet()
    
    [ERROR] High: Null passed for non-null parameter of
    java.util.Collections.addAll(Collection, Object[]) in
    org.apache.commons.compress.utils.Sets.newHashSet(Object[])
    [org.apache.commons.compress.utils.Sets,
    org.apache.commons.compress.utils.Sets] Method invoked at
    Sets.java:[line 42]Known null at Sets.java:[line 41] NP_NULL_PARAM_DEREF
---
 src/changes/changes.xml                            |  1 +
 .../org/apache/commons/compress/utils/Sets.java    |  4 ++-
 .../apache/commons/compress/utils/SetsTest.java}   | 35 ++++++++--------------
 3 files changed, 17 insertions(+), 23 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index bcf059fa2..51ec36732 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -58,6 +58,7 @@ The <action> type attribute can be add,update,fix,remove.
       <action type="fix" issue="COMPRESS-679" dev="ggregory" due-to="Mikaƫl MECHOULAM, Gary Gregory">Regression on parallel processing of 7zip files.</action>
       <action type="fix" issue="COMPRESS-675" dev="ggregory" due-to="Tim Allison, Gary Gregory">Regression in pack200's Archive class -- underlying InputStream is now closed.</action>
       <action type="fix" dev="ggregory" due-to="Gary Gregory">Avoid NullPointerException in java.nio.channels.spi.AbstractInterruptibleChannel.close() when calling org.apache.commons.compress.archivers.zip.FileRandomAccessOutputStream.close().</action>
+      <action type="fix" dev="ggregory" due-to="Gary Gregory">Fix SpotBugs NP_NULL_PARAM_DEREF in Sets.newHashSet().</action>
       <!-- UPDATE -->
       <action type="update" dev="ggregory" due-to="Dependabot, Gary Gregory">Bump org.apache.commons:commons-parent from 66 to 69 #495, #508.</action>
       <action type="update" dev="ggregory" due-to="Dependabot, Gary Gregory">Bump org.ow2.asm:asm from 9.6 to 9.7 #504.</action>
diff --git a/src/main/java/org/apache/commons/compress/utils/Sets.java b/src/main/java/org/apache/commons/compress/utils/Sets.java
index c2177642c..96282812b 100644
--- a/src/main/java/org/apache/commons/compress/utils/Sets.java
+++ b/src/main/java/org/apache/commons/compress/utils/Sets.java
@@ -39,7 +39,9 @@ public class Sets {
     @SafeVarargs
     public static <E> HashSet<E> newHashSet(final E... elements) {
         final HashSet<E> set = new HashSet<>(elements != null ? elements.length : 0);
-        Collections.addAll(set, elements);
+        if (elements != null) {
+            Collections.addAll(set, elements);
+        }
         return set;
     }
 
diff --git a/src/main/java/org/apache/commons/compress/utils/Sets.java b/src/test/java/org/apache/commons/compress/utils/SetsTest.java
similarity index 59%
copy from src/main/java/org/apache/commons/compress/utils/Sets.java
copy to src/test/java/org/apache/commons/compress/utils/SetsTest.java
index c2177642c..b0875bc3a 100644
--- a/src/main/java/org/apache/commons/compress/utils/Sets.java
+++ b/src/test/java/org/apache/commons/compress/utils/SetsTest.java
@@ -19,31 +19,22 @@
 
 package org.apache.commons.compress.utils;
 
-import java.util.Collections;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
 import java.util.HashSet;
 
-/**
- * Set utilities
- *
- * @since 1.13
- */
-public class Sets {
+import org.junit.jupiter.api.Test;
 
-    /**
-     * Creates a new HashSet filled with the given elements
-     *
-     * @param elements the elements to fill the new set
-     * @param <E>      type of elements contained in new set
-     * @return A new HasSet
-     */
-    @SafeVarargs
-    public static <E> HashSet<E> newHashSet(final E... elements) {
-        final HashSet<E> set = new HashSet<>(elements != null ? elements.length : 0);
-        Collections.addAll(set, elements);
-        return set;
-    }
+public class SetsTest {
 
-    private Sets() {
-        // Do not instantiate
+    @Test
+    public void testNewHashSet() {
+        final HashSet<String> expected = new HashSet<>();
+        assertEquals(expected, Sets.newHashSet());
+        assertEquals(expected, Sets.newHashSet((String[]) null));
+        expected.add("1");
+        assertEquals(expected, Sets.newHashSet("1"));
+        expected.add("2");
+        assertEquals(expected, Sets.newHashSet("2", "1"));
     }
 }