mirror of
https://github.com/openjdk/jdk.git
synced 2026-02-26 18:20:28 +00:00
8133022: Instant.toEpochMilli() silently overflows
Reviewed-by: lancea, chegar, simonis, dfuchs, igerasim
This commit is contained in:
parent
8145a4b457
commit
e2f7de320c
@ -1232,10 +1232,10 @@ public final class Instant
|
||||
if (seconds < 0 && nanos > 0) {
|
||||
long millis = Math.multiplyExact(seconds+1, 1000);
|
||||
long adjustment = nanos / 1000_000 - 1000;
|
||||
return millis + adjustment;
|
||||
return Math.addExact(millis, adjustment);
|
||||
} else {
|
||||
long millis = Math.multiplyExact(seconds, 1000);
|
||||
return millis + nanos / 1000_000;
|
||||
return Math.addExact(millis, nanos / 1000_000);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -112,6 +112,8 @@ import org.testng.annotations.Test;
|
||||
|
||||
/**
|
||||
* Test Instant.
|
||||
*
|
||||
* @bug 8133022
|
||||
*/
|
||||
@Test
|
||||
public class TCKInstant extends AbstractDateTimeTest {
|
||||
@ -1928,6 +1930,16 @@ public class TCKInstant extends AbstractDateTimeTest {
|
||||
Instant.ofEpochSecond(Long.MIN_VALUE / 1000 - 1).toEpochMilli();
|
||||
}
|
||||
|
||||
@Test(expectedExceptions=ArithmeticException.class)
|
||||
public void test_toEpochMillis_overflow() {
|
||||
Instant.ofEpochSecond(Long.MAX_VALUE / 1000, 809_000_000).toEpochMilli();
|
||||
}
|
||||
|
||||
@Test(expectedExceptions=ArithmeticException.class)
|
||||
public void test_toEpochMillis_overflow2() {
|
||||
Instant.ofEpochSecond(-9223372036854776L, 1).toEpochMilli();
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
// compareTo()
|
||||
//-----------------------------------------------------------------------
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user