Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8246788: ZoneRules invariants can be broken #2191

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/java.base/share/classes/java/time/zone/ZoneRules.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2021, 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
Expand Down Expand Up @@ -259,10 +259,12 @@ public static ZoneRules of(ZoneOffset offset) {
}

// last rules
if (lastRules.size() > 16) {
Object[] temp = lastRules.toArray();
ZoneOffsetTransitionRule[] rulesArray = Arrays.copyOf(temp, temp.length, ZoneOffsetTransitionRule[].class);
Comment on lines +262 to +263
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Could be replaced by:

ZoneOffsetTransitionRule[] rulesArray = (ZoneOffsetTransitionRule[])lastRules.toArray(new ZoneOffsetTransitionRule[0]).clone();

if you wanted - but what you currently have is good for me.

Copy link

@efge efge Jan 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or even maybe rulesArray = lastRules.toArray(ZoneOffsetTransitionRule[]::new);?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point - but that would be:

ZoneOffsetTransitionRule[] rulesArray = lastRules.toArray(ZoneOffsetTransitionRule[]::new).clone();

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. This last one is more concise, but it's a bit harder to reason about. The lastRules implementation could return an array of a type other than ZOTR[]. If it's unrelated or a supertype, this would result in ClassCastException -- probably not a problem. If it were a subtype of ZOTR[], this would get stored in the object's field. Is this a problem? Turns out it can't happen, since ZOTR is final. While not wrong, I don't think this is the right idiom.

It occurs to me that there should by another overload Arrays.copyOf(array, newType) that changes the type without changing the length. This would let us get rid of the local variable.

if (rulesArray.length > 16) {
throw new IllegalArgumentException("Too many transition rules");
}
this.lastRules = lastRules.toArray(new ZoneOffsetTransitionRule[lastRules.size()]);
this.lastRules = rulesArray;
}

/**
Expand Down
85 changes: 85 additions & 0 deletions test/jdk/java/time/test/java/time/zone/TestMutableZoneRules.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright (c) 2021, 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

package test.java.time.zone;

import java.time.*;
import java.time.zone.*;
import java.util.*;

import org.testng.annotations.Test;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertThrows;

/**
* @summary ZoneRules invariants can be broken.
*
* @bug 8246788
*/
@Test
public class TestMutableZoneRules {
static final ZoneOffset offset = ZoneOffset.ofHoursMinutes(1, 30);

static final ZoneOffsetTransitionRule rule1 =
ZoneOffsetTransitionRule.of(Month.APRIL, 2, DayOfWeek.TUESDAY, LocalTime.MIN, true,
ZoneOffsetTransitionRule.TimeDefinition.UTC, offset, offset, offset);

static final ZoneOffsetTransitionRule rule2 =
ZoneOffsetTransitionRule.of(Month.MARCH, 2, DayOfWeek.MONDAY, LocalTime.MIN, true,
ZoneOffsetTransitionRule.TimeDefinition.UTC, offset, offset, offset);

public void testMutation() {
ZoneOffsetTransitionRule[] array = { rule1 };
ZoneRules zr1 = ZoneRules.of(offset, offset, List.of(), List.of(), List.of(rule1));
ZoneRules zr2 = ZoneRules.of(offset, offset, List.of(), List.of(), new TestList(array, array.length));

assertEquals(zr2, zr1);
array[0] = rule2;
assertEquals(zr2, zr1);
}

public void testLength() {
ZoneOffsetTransitionRule[] array = new ZoneOffsetTransitionRule[17];
Arrays.setAll(array, i -> rule1);

assertThrows(IllegalArgumentException.class,
() -> ZoneRules.of(offset, offset, List.of(), List.of(), new TestList(array, 1)));
}

static class TestList extends AbstractList<ZoneOffsetTransitionRule> {
final ZoneOffsetTransitionRule[] array;
final int size;

TestList(ZoneOffsetTransitionRule[] array, int size) {
this.array = array;
this.size = size;
}

public int size() { return size; }
public ZoneOffsetTransitionRule get(int i) { return array[i]; }
public Object[] toArray() { return array; }

@SuppressWarnings("unchecked")
public <T> T[] toArray(T[] a) { return (T[]) array; }
}
}