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:52:32 UTC
[commons-text] branch master updated: Add null-check in RandomStringGenerator#Builder#withinRange() to avoid NullPointerException
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 f9da0b28 Add null-check in RandomStringGenerator#Builder#withinRange() to avoid NullPointerException
f9da0b28 is described below
commit f9da0b283b616b8076e0bbad03cdd775c786a072
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Tue Jun 27 08:51:52 2023 -0400
Add null-check in RandomStringGenerator#Builder#withinRange() to avoid
NullPointerException
---
src/changes/changes.xml | 3 ++-
.../apache/commons/text/RandomStringGenerator.java | 20 +++++++++++---------
.../commons/text/RandomStringGeneratorTest.java | 7 ++++++-
3 files changed, 19 insertions(+), 11 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 87329deb..687dd633 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -60,7 +60,8 @@ The <action> type attribute can be add,update,fix,remove.
<action type="fix" dev="ggregory" due-to="step-security-bot, Gary Gregory">[StepSecurity] ci: Harden GitHub Actions #428.</action>
<action type="fix" dev="ggregory" due-to="Dimitrios Efthymiou">Improve AlphabetConverter Javadoc #429.</action>
<action type="fix" dev="ggregory" due-to="Dimitrios Efthymiou">Fix exception message in IntersectionResult to make set-theoretic sense #438.</action>
- <action type="fix" dev="ggregory" due-to="Dimitrios Efthymiou, Gary Gregory">Add null-check in RandomStringGenerator#selectFrom() to avoid NullPointerException #434.</action>
+ <action type="fix" dev="ggregory" due-to="Dimitrios Efthymiou, Gary Gregory">Add null-check in RandomStringGenerator#Builder#selectFrom() to avoid NullPointerException #434.</action>
+ <action type="fix" dev="ggregory" due-to="Dimitrios Efthymiou, Gary Gregory">Add null-check in RandomStringGenerator#Builder#withinRange() to avoid NullPointerException.</action>
<!-- ADD -->
<action issue="TEXT-224" type="add" dev="ggregory" due-to="PJ Fanning, Gary Gregory">Set SecureProcessing feature in XmlStringLookup by default.</action>
<action issue="TEXT-224" type="add" dev="ggregory" due-to="Gary Gregory">Add StringLookupFactory.xmlStringLookup(Map<String, Boolean>...).</action>
diff --git a/src/main/java/org/apache/commons/text/RandomStringGenerator.java b/src/main/java/org/apache/commons/text/RandomStringGenerator.java
index 16f1f5d5..0a559bf5 100644
--- a/src/main/java/org/apache/commons/text/RandomStringGenerator.java
+++ b/src/main/java/org/apache/commons/text/RandomStringGenerator.java
@@ -244,15 +244,17 @@ public final class RandomStringGenerator {
*/
public Builder withinRange(final char[]... pairs) {
characterList = new ArrayList<>();
- for (final char[] pair : pairs) {
- Validate.isTrue(pair.length == 2, "Each pair must contain minimum and maximum code point");
- final int minimumCodePoint = pair[0];
- final int maximumCodePoint = pair[1];
- Validate.isTrue(minimumCodePoint <= maximumCodePoint, "Minimum code point %d is larger than maximum code point %d", minimumCodePoint,
- maximumCodePoint);
-
- for (int index = minimumCodePoint; index <= maximumCodePoint; index++) {
- characterList.add((char) index);
+ if (pairs != null) {
+ for (final char[] pair : pairs) {
+ Validate.isTrue(pair.length == 2, "Each pair must contain minimum and maximum code point");
+ final int minimumCodePoint = pair[0];
+ final int maximumCodePoint = pair[1];
+ Validate.isTrue(minimumCodePoint <= maximumCodePoint, "Minimum code point %d is larger than maximum code point %d", minimumCodePoint,
+ maximumCodePoint);
+
+ for (int index = minimumCodePoint; index <= maximumCodePoint; index++) {
+ characterList.add((char) index);
+ }
}
}
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 9895500c..400c1e3a 100644
--- a/src/test/java/org/apache/commons/text/RandomStringGeneratorTest.java
+++ b/src/test/java/org/apache/commons/text/RandomStringGeneratorTest.java
@@ -300,8 +300,13 @@ public class RandomStringGeneratorTest {
public void testWithinMultipleRanges() {
final int length = 5000;
final char[][] pairs = {{'a', 'z'}, {'0', '9'}};
+ // @formatter:off
final RandomStringGenerator generator = RandomStringGenerator.builder()
- .withinRange(pairs).build();
+ .withinRange()
+ .withinRange(null)
+ .withinRange(pairs)
+ .build();
+ // @formatter:on
final String str = generator.generate(length);
int minimumCodePoint = 0, maximumCodePoint = 0;