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 2023/06/27 12:36:20 UTC

[commons-text] branch master updated: Add null-check in RandomStringGenerator#selectFrom() to avoid NullPointerException (#434)

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


The following commit(s) were added to refs/heads/master by this push:
     new ce20118e Add null-check in RandomStringGenerator#selectFrom() to avoid NullPointerException (#434)
ce20118e is described below

commit ce20118e7c2690306ecaadd2a130657299eee16c
Author: Dimitrios Efthymiou <ef...@gmail.com>
AuthorDate: Tue Jun 27 13:36:13 2023 +0100

    Add null-check in RandomStringGenerator#selectFrom() to avoid NullPointerException (#434)
    
    * added null-check to avoid NullPointerException
    
    * Update RandomStringGenerator.java
    
    * --I made selectFrom reset the allowed characters when the method is called, regardless of the method input
    --I changed the test to assert the length of the random text
    
    * added test for selectFrom passing null varargs
---
 src/main/java/org/apache/commons/text/RandomStringGenerator.java  | 6 ++++--
 .../java/org/apache/commons/text/RandomStringGeneratorTest.java   | 8 ++++++++
 2 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/src/main/java/org/apache/commons/text/RandomStringGenerator.java b/src/main/java/org/apache/commons/text/RandomStringGenerator.java
index ead0148f..8ae6d002 100644
--- a/src/main/java/org/apache/commons/text/RandomStringGenerator.java
+++ b/src/main/java/org/apache/commons/text/RandomStringGenerator.java
@@ -184,8 +184,10 @@ public final class RandomStringGenerator {
          */
         public Builder selectFrom(final char... chars) {
             characterList = new ArrayList<>();
-            for (final char c : chars) {
-                characterList.add(c);
+            if (chars != null) {
+                for (final char c : chars) {
+                    characterList.add(c);
+                }
             }
             return this;
         }
diff --git a/src/test/java/org/apache/commons/text/RandomStringGeneratorTest.java b/src/test/java/org/apache/commons/text/RandomStringGeneratorTest.java
index 927fe853..4228e6e6 100644
--- a/src/test/java/org/apache/commons/text/RandomStringGeneratorTest.java
+++ b/src/test/java/org/apache/commons/text/RandomStringGeneratorTest.java
@@ -250,6 +250,14 @@ public class RandomStringGeneratorTest {
         }
     }
 
+    @Test
+    public void testSelectFromNullCharVarargs() {
+        final int length = 5;
+        final RandomStringGenerator generator = new RandomStringGenerator.Builder().selectFrom(null).build();
+        final String randomText = generator.generate(length);
+        assertThat(codePointLength(randomText)).isEqualTo(length);
+    }
+
     @Test
     public void testSetLength() {
         final int length = 99;