Issue | Fix Version | Assignee | Priority | Status | Resolution | Resolved In Build |
---|---|---|---|---|---|---|
JDK-8136075 | emb-9 | Roger Riggs | P4 | Resolved | Fixed | team |
JDK-8140884 | 8u91 | Ivan Gerasimov | P4 | Resolved | Fixed | b01 |
JDK-8133204 | 8u72 | Ivan Gerasimov | P4 | Resolved | Fixed | b01 |
JDK-8147216 | emb-8u91 | Ivan Gerasimov | P4 | Resolved | Fixed | b01 |
FULL PRODUCT VERSION :
1.8.0_45-b15
ADDITIONAL OS VERSION INFORMATION :
Any OS
A DESCRIPTION OF THE PROBLEM :
This code should throw an ArithmeticException but silently overflows:
Instant tooMuch = Instant.ofEpochSecond(Long.MAX_VALUE / 1000, 809_000_000);
System.out.println(tooMuch.toEpochMilli()); //prints -9223372036854775807
The problem is in the code of toEpochMilli:
public long toEpochMilli() {
long millis = Math.multiplyExact(seconds, 1000);
return millis + nanos / 1000_000;
}
which should probably be:
public long toEpochMilli() {
long millis = Math.multiplyExact(seconds, 1000);
return Math.addExact(millis, nanos / 1000_000);
}
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The code should throw an ArithmeticException
ACTUAL -
The result is a negative number due to overflow
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
public static void main(String args[]) {
Instant tooMuch = Instant.ofEpochSecond(Long.MAX_VALUE / 1000, 809_000_000);
System.out.println(tooMuch.toEpochMilli()); //prints -9223372036854775807
}
---------- END SOURCE ----------
1.8.0_45-b15
ADDITIONAL OS VERSION INFORMATION :
Any OS
A DESCRIPTION OF THE PROBLEM :
This code should throw an ArithmeticException but silently overflows:
Instant tooMuch = Instant.ofEpochSecond(Long.MAX_VALUE / 1000, 809_000_000);
System.out.println(tooMuch.toEpochMilli()); //prints -9223372036854775807
The problem is in the code of toEpochMilli:
public long toEpochMilli() {
long millis = Math.multiplyExact(seconds, 1000);
return millis + nanos / 1000_000;
}
which should probably be:
public long toEpochMilli() {
long millis = Math.multiplyExact(seconds, 1000);
return Math.addExact(millis, nanos / 1000_000);
}
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The code should throw an ArithmeticException
ACTUAL -
The result is a negative number due to overflow
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
public static void main(String args[]) {
Instant tooMuch = Instant.ofEpochSecond(Long.MAX_VALUE / 1000, 809_000_000);
System.out.println(tooMuch.toEpochMilli()); //prints -9223372036854775807
}
---------- END SOURCE ----------
- backported by
-
JDK-8133204 Instant.toEpochMilli() silently overflows
-
- Resolved
-
-
JDK-8136075 Instant.toEpochMilli() silently overflows
-
- Resolved
-
-
JDK-8140884 Instant.toEpochMilli() silently overflows
-
- Resolved
-
-
JDK-8147216 Instant.toEpochMilli() silently overflows
-
- Resolved
-