Skip to content

Commit 3741c98

Browse files
author
Justin Lu
committedFeb 13, 2025
8349883: Locale.LanguageRange.parse("-") throws ArrayIndexOutOfBoundsException
Reviewed-by: naoto
1 parent 3e7acfa commit 3741c98

File tree

3 files changed

+71
-77
lines changed

3 files changed

+71
-77
lines changed
 

‎src/java.base/share/classes/java/util/Locale.java

+3-5
Original file line numberDiff line numberDiff line change
@@ -3266,9 +3266,7 @@ public LanguageRange(String range) {
32663266
* or greater than {@code MAX_WEIGHT}
32673267
*/
32683268
public LanguageRange(String range, double weight) {
3269-
if (range == null) {
3270-
throw new NullPointerException();
3271-
}
3269+
Objects.requireNonNull(range);
32723270
if (weight < MIN_WEIGHT || weight > MAX_WEIGHT) {
32733271
throw new IllegalArgumentException("weight=" + weight);
32743272
}
@@ -3278,8 +3276,8 @@ public LanguageRange(String range, double weight) {
32783276
// Do syntax check.
32793277
boolean isIllFormed = false;
32803278
String[] subtags = range.split("-");
3281-
if (isSubtagIllFormed(subtags[0], true)
3282-
|| range.endsWith("-")) {
3279+
if (range.endsWith("-") ||
3280+
isSubtagIllFormed(subtags[0], true)) {
32833281
isIllFormed = true;
32843282
} else {
32853283
for (int i = 1; i < subtags.length; i++) {

‎test/jdk/java/util/Locale/LRToString.java

-64
This file was deleted.

‎test/jdk/java/util/Locale/LanguageRangeTest.java

+68-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2020, 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
@@ -20,21 +20,62 @@
2020
* or visit www.oracle.com if you need additional information or have any
2121
* questions.
2222
*/
23-
/**
23+
24+
/*
2425
* @test
25-
* @bug 8253321
26-
* @summary test LanguageRange class
27-
* @run testng LanguageRangeTest
26+
* @bug 8026766 8253321 8349883
27+
* @summary LanguageRange tests: toString(), hashCode()/equals(), checking
28+
* for IAE on ill-formed ranges
29+
* @run junit LanguageRangeTest
2830
*/
2931

3032
import static java.util.Locale.LanguageRange;
3133

32-
import org.testng.annotations.Test;
33-
import static org.testng.Assert.assertEquals;
34+
import org.junit.jupiter.api.Test;
35+
import org.junit.jupiter.params.ParameterizedTest;
36+
import org.junit.jupiter.params.provider.Arguments;
37+
import org.junit.jupiter.params.provider.MethodSource;
38+
39+
import java.util.HashMap;
40+
import java.util.Locale;
41+
import java.util.stream.Stream;
42+
43+
import static org.junit.jupiter.api.Assertions.assertEquals;
44+
import static org.junit.jupiter.api.Assertions.assertThrows;
3445

35-
@Test
3646
public class LanguageRangeTest {
3747

48+
// 8349883: Test endpoints w/ ill-formed language range fail with IAE
49+
@ParameterizedTest
50+
@MethodSource("illegalRanges")
51+
public void illformedRangeTest(String range) {
52+
// static parses
53+
assertThrows(IllegalArgumentException.class,
54+
() -> Locale.LanguageRange.parse(range));
55+
assertThrows(IllegalArgumentException.class,
56+
() -> Locale.LanguageRange.parse(range, new HashMap<>()));
57+
// ctors
58+
assertThrows(IllegalArgumentException.class,
59+
() -> new Locale.LanguageRange(range));
60+
assertThrows(IllegalArgumentException.class,
61+
() -> new Locale.LanguageRange(range, Locale.LanguageRange.MIN_WEIGHT));
62+
}
63+
64+
private static Stream<String> illegalRanges() {
65+
return Stream.of(
66+
// 8349883 offending range
67+
"-",
68+
// Other general ill-formed test cases
69+
"-foo",
70+
"foo-",
71+
"foo1",
72+
"foo-123456789",
73+
"*-*-",
74+
""
75+
);
76+
}
77+
78+
// 8253321: Ensure invoking hashCode does not affect equals result
3879
@Test
3980
public void hashCodeTest() {
4081
var range1 = new LanguageRange("en-GB", 0);
@@ -45,4 +86,23 @@ public void hashCodeTest() {
4586
range2.hashCode();
4687
assertEquals(range1, range2);
4788
}
89+
90+
// 8026766: toString() should hide weight if equal to MAX_WEIGHT (1.0)
91+
@ParameterizedTest
92+
@MethodSource("ranges")
93+
public void toStringTest(String range, double weight) {
94+
LanguageRange lr = new LanguageRange(range, weight);
95+
String expected = weight == 1.0
96+
? range
97+
: range+";q="+weight;
98+
assertEquals(lr.toString(), expected);
99+
}
100+
101+
private static Stream<Arguments> ranges() {
102+
return Stream.of(
103+
Arguments.of("ja", 1.0),
104+
Arguments.of("de", 0.5),
105+
Arguments.of("fr", 0.0)
106+
);
107+
}
48108
}

0 commit comments

Comments
 (0)
Please sign in to comment.