Skip to content

Commit a887177

Browse files
author
Stuart Marks
committed
8246788: ZoneRules invariants can be broken
Reviewed-by: rriggs, naoto
1 parent 874aef4 commit a887177

File tree

2 files changed

+90
-3
lines changed

2 files changed

+90
-3
lines changed

src/java.base/share/classes/java/time/zone/ZoneRules.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2021, 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
@@ -259,10 +259,12 @@ public static ZoneRules of(ZoneOffset offset) {
259259
}
260260

261261
// last rules
262-
if (lastRules.size() > 16) {
262+
Object[] temp = lastRules.toArray();
263+
ZoneOffsetTransitionRule[] rulesArray = Arrays.copyOf(temp, temp.length, ZoneOffsetTransitionRule[].class);
264+
if (rulesArray.length > 16) {
263265
throw new IllegalArgumentException("Too many transition rules");
264266
}
265-
this.lastRules = lastRules.toArray(new ZoneOffsetTransitionRule[lastRules.size()]);
267+
this.lastRules = rulesArray;
266268
}
267269

268270
/**
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright (c) 2021, 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+
package test.java.time.zone;
25+
26+
import java.time.*;
27+
import java.time.zone.*;
28+
import java.util.*;
29+
30+
import org.testng.annotations.Test;
31+
import static org.testng.Assert.assertEquals;
32+
import static org.testng.Assert.assertThrows;
33+
34+
/**
35+
* @summary ZoneRules invariants can be broken.
36+
*
37+
* @bug 8246788
38+
*/
39+
@Test
40+
public class TestMutableZoneRules {
41+
static final ZoneOffset offset = ZoneOffset.ofHoursMinutes(1, 30);
42+
43+
static final ZoneOffsetTransitionRule rule1 =
44+
ZoneOffsetTransitionRule.of(Month.APRIL, 2, DayOfWeek.TUESDAY, LocalTime.MIN, true,
45+
ZoneOffsetTransitionRule.TimeDefinition.UTC, offset, offset, offset);
46+
47+
static final ZoneOffsetTransitionRule rule2 =
48+
ZoneOffsetTransitionRule.of(Month.MARCH, 2, DayOfWeek.MONDAY, LocalTime.MIN, true,
49+
ZoneOffsetTransitionRule.TimeDefinition.UTC, offset, offset, offset);
50+
51+
public void testMutation() {
52+
ZoneOffsetTransitionRule[] array = { rule1 };
53+
ZoneRules zr1 = ZoneRules.of(offset, offset, List.of(), List.of(), List.of(rule1));
54+
ZoneRules zr2 = ZoneRules.of(offset, offset, List.of(), List.of(), new TestList(array, array.length));
55+
56+
assertEquals(zr2, zr1);
57+
array[0] = rule2;
58+
assertEquals(zr2, zr1);
59+
}
60+
61+
public void testLength() {
62+
ZoneOffsetTransitionRule[] array = new ZoneOffsetTransitionRule[17];
63+
Arrays.setAll(array, i -> rule1);
64+
65+
assertThrows(IllegalArgumentException.class,
66+
() -> ZoneRules.of(offset, offset, List.of(), List.of(), new TestList(array, 1)));
67+
}
68+
69+
static class TestList extends AbstractList<ZoneOffsetTransitionRule> {
70+
final ZoneOffsetTransitionRule[] array;
71+
final int size;
72+
73+
TestList(ZoneOffsetTransitionRule[] array, int size) {
74+
this.array = array;
75+
this.size = size;
76+
}
77+
78+
public int size() { return size; }
79+
public ZoneOffsetTransitionRule get(int i) { return array[i]; }
80+
public Object[] toArray() { return array; }
81+
82+
@SuppressWarnings("unchecked")
83+
public <T> T[] toArray(T[] a) { return (T[]) array; }
84+
}
85+
}

0 commit comments

Comments
 (0)