Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8133022

Instant.toEpochMilli() silently overflows

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: P4
    • Resolution: Fixed
    • Affects Version/s: 8u45, 9
    • Fix Version/s: 9
    • Component/s: core-libs
    • Labels:
    • Subcomponent:
    • Resolved In Build:
      b77
    • CPU:
      x86
    • OS:
      windows_8

      Backports

        Description

        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. Main.java
          0.3 kB
          Pardeep Sharma

          Activity

          Hide
          pardesha Pardeep Sharma added a comment -
          - Run the attached test case in Windows 7.

          - Checked this for JDK 8u45, 8u51, 8u60 ea b26 and 9 ea b75 and could conform the issue.

          - Output with JDK 8u51:
          > java Main
          -9223372036854775807

          - Moving this up for further evaluation.
          Show
          pardesha Pardeep Sharma added a comment - - Run the attached test case in Windows 7. - Checked this for JDK 8u45, 8u51, 8u60 ea b26 and 9 ea b75 and could conform the issue. - Output with JDK 8u51: > java Main -9223372036854775807 - Moving this up for further evaluation.
          Hide
          scolebourne Stephen Colebourne added a comment -
          Yes, this looks like a bug. The proposed fix would be the correct one.
          Show
          scolebourne Stephen Colebourne added a comment - Yes, this looks like a bug. The proposed fix would be the correct one.
          Hide
          rriggs Roger Riggs added a comment -
          Another test case:
                 Instant inst = Instant.ofEpochSecond(-9223372036854776L, 1);
                  System.out.println(inst.toEpochMilli());
          Show
          rriggs Roger Riggs added a comment - Another test case:        Instant inst = Instant.ofEpochSecond(-9223372036854776L, 1);         System.out.println(inst.toEpochMilli());
          Hide
          hgupdate HG Updates added a comment -
          URL: http://hg.openjdk.java.net/jdk9/dev/jdk/rev/17577017dfe2
          User: rriggs
          Date: 2015-08-06 18:35:21 +0000
          Show
          hgupdate HG Updates added a comment - URL: http://hg.openjdk.java.net/jdk9/dev/jdk/rev/17577017dfe2 User: rriggs Date: 2015-08-06 18:35:21 +0000
          Hide
          hgupdate HG Updates added a comment -
          URL: http://hg.openjdk.java.net/jdk9/jdk9/jdk/rev/17577017dfe2
          User: lana
          Date: 2015-08-11 20:21:52 +0000
          Show
          hgupdate HG Updates added a comment - URL: http://hg.openjdk.java.net/jdk9/jdk9/jdk/rev/17577017dfe2 User: lana Date: 2015-08-11 20:21:52 +0000

            People

            • Assignee:
              rriggs Roger Riggs
              Reporter:
              webbuggrp Webbug Group
            • Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: