8350646: Calendar.Builder.build() Throws ArrayIndexOutOfBoundsException

Reviewed-by: naoto
This commit is contained in:
Justin Lu 2025-02-28 19:37:36 +00:00
parent 197004f4c6
commit 3a7d986878
4 changed files with 36 additions and 6 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2005, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -1856,6 +1856,14 @@ class JapaneseImperialCalendar extends Calendar {
if (isSet(ERA)) {
era = internalGet(ERA);
// Don't check under, historically we have allowed values under
// BEFORE_MEIJI to be ignored during normalization
// We check against eras.length (not the highest constant ERA value)
// due to future added eras, or additional eras via
// "jdk.calendar.japanese.supplemental.era"
if (era >= eras.length) {
throw new IllegalArgumentException("Invalid era");
}
year = isSet(YEAR) ? internalGet(YEAR) : 1;
} else {
if (isSet(YEAR)) {

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -23,8 +23,9 @@
/*
* @test
* @bug 4745761
* @bug 4745761 8350646
* @summary Unit test for Calendar.Builder.
* @run main BuilderTest
*/
import java.time.LocalDateTime;
@ -245,6 +246,11 @@ public class BuilderTest {
checkException(calb, IllegalArgumentException.class);
calb = builder().setCalendarType("japanese").setWeekDate(2013, 1, MONDAY);
checkException(calb, IllegalArgumentException.class);
// JDK-8350646 : Ensure IAE (instead of AIOOBE) for ERA over largest supported
calb = builder().setCalendarType("japanese").setFields(ERA, 6);
checkException(calb, IllegalArgumentException.class);
// Note that we don't check ERAs under BEFORE_MEIJI, i.e. -1, -2, ... as
// historically JapaneseImperialCalendar ignores such values when normalizing
}
private static Calendar.Builder builder() {

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -67,6 +67,22 @@ public class SupplementalJapaneseEraTest {
}
private static void testProperty() {
// JDK-8350646: Ensure that IAE is thrown for out of range era, when
// additional era is defined
try {
new Calendar.Builder()
.setCalendarType("japanese")
.setFields(ERA, JapaneseEra.values().length + 2)
.build();
System.err.println("Out of range era should have thrown IAE");
errors++;
} catch (Exception e) {
if (!(e instanceof IllegalArgumentException)) {
System.err.printf("Out of range era threw \"%s\" instead of IAE\n", e);
errors++;
}
}
Calendar jcal = new Calendar.Builder()
.setCalendarType("japanese")
.setFields(ERA, 6, YEAR, 1, DAY_OF_YEAR, 1)

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -23,7 +23,7 @@
/*
* @test
* @bug 8048123 8054214 8173423
* @bug 8048123 8054214 8173423 8350646
* @summary Test for jdk.calendar.japanese.supplemental.era support
* @library /test/lib
* @build SupplementalJapaneseEraTest