8074032: Instant.ofEpochMilli(millis).toEpochMilli() can throw arithmetic overflow in toEpochMilli()
Instant.toEpochMilli() now takes into account the sign of the 'seconds' field. Reviewed-by: rriggs, scolebourne
This commit is contained in:
parent
6de0c1df26
commit
9b5f69fa24
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
@ -1229,9 +1229,15 @@ public final class Instant
|
|||||||
* @throws ArithmeticException if numeric overflow occurs
|
* @throws ArithmeticException if numeric overflow occurs
|
||||||
*/
|
*/
|
||||||
public long toEpochMilli() {
|
public long toEpochMilli() {
|
||||||
|
if (seconds < 0 && nanos > 0) {
|
||||||
|
long millis = Math.multiplyExact(seconds+1, 1000);
|
||||||
|
long adjustment = nanos / 1000_000 - 1000;
|
||||||
|
return millis + adjustment;
|
||||||
|
} else {
|
||||||
long millis = Math.multiplyExact(seconds, 1000);
|
long millis = Math.multiplyExact(seconds, 1000);
|
||||||
return millis + nanos / 1000_000;
|
return millis + nanos / 1000_000;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//-----------------------------------------------------------------------
|
//-----------------------------------------------------------------------
|
||||||
/**
|
/**
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
@ -62,6 +62,8 @@ package test.java.time;
|
|||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
|
|
||||||
import org.testng.annotations.Test;
|
import org.testng.annotations.Test;
|
||||||
|
import org.testng.annotations.DataProvider;
|
||||||
|
import static org.testng.Assert.assertEquals;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test Instant.
|
* Test Instant.
|
||||||
@ -74,4 +76,24 @@ public class TestInstant extends AbstractTest {
|
|||||||
assertImmutable(Instant.class);
|
assertImmutable(Instant.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@DataProvider(name="sampleEpochMillis")
|
||||||
|
private Object[][] provider_sampleEpochMillis() {
|
||||||
|
return new Object[][] {
|
||||||
|
{"Long.MAX_VALUE", Long.MAX_VALUE},
|
||||||
|
{"Long.MAX_VALUE-1", Long.MAX_VALUE - 1},
|
||||||
|
{"1", 1L},
|
||||||
|
{"0", 0L},
|
||||||
|
{"-1", -1L},
|
||||||
|
{"Long.MIN_VALUE+1", Long.MIN_VALUE + 1},
|
||||||
|
{"Long.MIN_VALUE", Long.MIN_VALUE}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(dataProvider="sampleEpochMillis")
|
||||||
|
public void test_epochMillis(String name, long millis) {
|
||||||
|
Instant t1 = Instant.ofEpochMilli(millis);
|
||||||
|
long m = t1.toEpochMilli();
|
||||||
|
assertEquals(millis, m, name);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user