Skip to content

Commit c3b4819

Browse files
author
Justin Lu
committed
8351074: Disallow null prefix and suffix in DecimalFormat
Reviewed-by: naoto
1 parent 6012e8d commit c3b4819

File tree

2 files changed

+64
-5
lines changed

2 files changed

+64
-5
lines changed

src/java.base/share/classes/java/text/DecimalFormat.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1996, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1996, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -48,6 +48,7 @@
4848
import java.util.ArrayList;
4949
import java.util.Currency;
5050
import java.util.Locale;
51+
import java.util.Objects;
5152
import java.util.concurrent.atomic.AtomicInteger;
5253
import java.util.concurrent.atomic.AtomicLong;
5354

@@ -2811,9 +2812,11 @@ public String getPositivePrefix () {
28112812
* Set the positive prefix.
28122813
* <P>Examples: +123, $123, sFr123
28132814
*
2814-
* @param newValue the new positive prefix
2815+
* @param newValue the new positive prefix. Non-null.
2816+
* @throws NullPointerException if {@code newValue} is {@code null}
28152817
*/
28162818
public void setPositivePrefix (String newValue) {
2819+
Objects.requireNonNull(newValue, "prefix must not be null");
28172820
positivePrefix = newValue;
28182821
posPrefixPattern = null;
28192822
positivePrefixFieldPositions = null;
@@ -2853,9 +2856,11 @@ public String getNegativePrefix () {
28532856
* Set the negative prefix.
28542857
* <P>Examples: -123, ($123) (with negative suffix), sFr-123
28552858
*
2856-
* @param newValue the new negative prefix
2859+
* @param newValue the new negative prefix. Non-null.
2860+
* @throws NullPointerException if {@code newValue} is {@code null}
28572861
*/
28582862
public void setNegativePrefix (String newValue) {
2863+
Objects.requireNonNull(newValue, "prefix must not be null");
28592864
negativePrefix = newValue;
28602865
negPrefixPattern = null;
28612866
fastPathCheckNeeded = true;
@@ -2894,9 +2899,11 @@ public String getPositiveSuffix () {
28942899
* Set the positive suffix.
28952900
* <P>Example: 123%
28962901
*
2897-
* @param newValue the new positive suffix
2902+
* @param newValue the new positive suffix. Non-null.
2903+
* @throws NullPointerException if {@code newValue} is {@code null}
28982904
*/
28992905
public void setPositiveSuffix (String newValue) {
2906+
Objects.requireNonNull(newValue, "suffix must not be null");
29002907
positiveSuffix = newValue;
29012908
posSuffixPattern = null;
29022909
fastPathCheckNeeded = true;
@@ -2935,9 +2942,11 @@ public String getNegativeSuffix () {
29352942
* Set the negative suffix.
29362943
* <P>Examples: 123%
29372944
*
2938-
* @param newValue the new negative suffix
2945+
* @param newValue the new negative suffix. Non-null.
2946+
* @throws NullPointerException if {@code newValue} is {@code null}
29392947
*/
29402948
public void setNegativeSuffix (String newValue) {
2949+
Objects.requireNonNull(newValue, "suffix must not be null");
29412950
negativeSuffix = newValue;
29422951
negSuffixPattern = null;
29432952
fastPathCheckNeeded = true;
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 8351074
27+
* @summary Test input value check for DecimalFormat affix setter methods
28+
* @run junit AffixTest
29+
*/
30+
31+
import org.junit.jupiter.api.Test;
32+
33+
import java.text.DecimalFormat;
34+
35+
import static org.junit.jupiter.api.Assertions.assertThrows;
36+
37+
public class AffixTest {
38+
39+
@Test
40+
public void nullPrefixTest() {
41+
assertThrows(NullPointerException.class, () -> new DecimalFormat().setPositivePrefix(null));
42+
assertThrows(NullPointerException.class, () -> new DecimalFormat().setNegativePrefix(null));
43+
}
44+
45+
@Test
46+
public void nullSuffixTest() {
47+
assertThrows(NullPointerException.class, () -> new DecimalFormat().setPositiveSuffix(null));
48+
assertThrows(NullPointerException.class, () -> new DecimalFormat().setNegativeSuffix(null));
49+
}
50+
}

0 commit comments

Comments
 (0)