Skip to content

Commit 3a7d986

Browse files
author
Justin Lu
committed
8350646: Calendar.Builder.build() Throws ArrayIndexOutOfBoundsException
Reviewed-by: naoto
1 parent 197004f commit 3a7d986

File tree

4 files changed

+36
-6
lines changed

4 files changed

+36
-6
lines changed

src/java.base/share/classes/java/util/JapaneseImperialCalendar.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2005, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2005, 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
@@ -1856,6 +1856,14 @@ protected void computeTime() {
18561856

18571857
if (isSet(ERA)) {
18581858
era = internalGet(ERA);
1859+
// Don't check under, historically we have allowed values under
1860+
// BEFORE_MEIJI to be ignored during normalization
1861+
// We check against eras.length (not the highest constant ERA value)
1862+
// due to future added eras, or additional eras via
1863+
// "jdk.calendar.japanese.supplemental.era"
1864+
if (era >= eras.length) {
1865+
throw new IllegalArgumentException("Invalid era");
1866+
}
18591867
year = isSet(YEAR) ? internalGet(YEAR) : 1;
18601868
} else {
18611869
if (isSet(YEAR)) {

test/jdk/java/util/Calendar/Builder/BuilderTest.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 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
@@ -23,8 +23,9 @@
2323

2424
/*
2525
* @test
26-
* @bug 4745761
26+
* @bug 4745761 8350646
2727
* @summary Unit test for Calendar.Builder.
28+
* @run main BuilderTest
2829
*/
2930

3031
import java.time.LocalDateTime;
@@ -245,6 +246,11 @@ private static void testExceptions() {
245246
checkException(calb, IllegalArgumentException.class);
246247
calb = builder().setCalendarType("japanese").setWeekDate(2013, 1, MONDAY);
247248
checkException(calb, IllegalArgumentException.class);
249+
// JDK-8350646 : Ensure IAE (instead of AIOOBE) for ERA over largest supported
250+
calb = builder().setCalendarType("japanese").setFields(ERA, 6);
251+
checkException(calb, IllegalArgumentException.class);
252+
// Note that we don't check ERAs under BEFORE_MEIJI, i.e. -1, -2, ... as
253+
// historically JapaneseImperialCalendar ignores such values when normalizing
248254
}
249255

250256
private static Calendar.Builder builder() {

test/jdk/java/util/Calendar/SupplementalJapaneseEraTest.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2014, 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
@@ -67,6 +67,22 @@ public static void main(String[] args) {
6767
}
6868

6969
private static void testProperty() {
70+
// JDK-8350646: Ensure that IAE is thrown for out of range era, when
71+
// additional era is defined
72+
try {
73+
new Calendar.Builder()
74+
.setCalendarType("japanese")
75+
.setFields(ERA, JapaneseEra.values().length + 2)
76+
.build();
77+
System.err.println("Out of range era should have thrown IAE");
78+
errors++;
79+
} catch (Exception e) {
80+
if (!(e instanceof IllegalArgumentException)) {
81+
System.err.printf("Out of range era threw \"%s\" instead of IAE\n", e);
82+
errors++;
83+
}
84+
}
85+
7086
Calendar jcal = new Calendar.Builder()
7187
.setCalendarType("japanese")
7288
.setFields(ERA, 6, YEAR, 1, DAY_OF_YEAR, 1)

test/jdk/java/util/Calendar/SupplementalJapaneseEraTestRun.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2014, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2014, 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
@@ -23,7 +23,7 @@
2323

2424
/*
2525
* @test
26-
* @bug 8048123 8054214 8173423
26+
* @bug 8048123 8054214 8173423 8350646
2727
* @summary Test for jdk.calendar.japanese.supplemental.era support
2828
* @library /test/lib
2929
* @build SupplementalJapaneseEraTest

0 commit comments

Comments
 (0)