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

Instant.ofEpochMilli(millis).toEpochMilli() can throw arithmetic overflow in toEpochMilli()

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: P3
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 9
    • Component/s: core-libs
    • Labels:
      None
    • Subcomponent:
    • Resolved In Build:
      b54

      Backports

        Description

        The following test:

                for (Object[] test : 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}
                    }) {

                    long millis = (Long)test[1];
                    System.err.println("Testing with " + test[0] +": "+millis+"L");
                    Instant i1 = Instant.ofEpochMilli(millis);
                    System.err.println("Instant.ofEpochMilli(): " + i1);
                    long seconds = i1.getEpochSecond();
                    System.err.println("Instant.getEpochSecond(): " + seconds);
                    System.err.println("millis/1000: " + millis/1000);

                    long millis2 = i1.toEpochMilli();
                    System.err.println("Instant.toEpochMilli(): " + millis2+"L");
                    if (millis2 != millis) {
                        throw new RuntimeException("Test failed for " + test[0]
                                + "\n\texpected: "+millis+"L"
                                + "\n\tactual: "+millis2+"L");
                    }
                }

        causes an arithmetic overflow for Long.MIN_VALUE+1 and Long.MIN_VALUE.

        Exception in thread "main" java.lang.ArithmeticException: long overflow
        at java.lang.Math.multiplyExact(Math.java:915)
        at java.time.Instant.toEpochMilli(Instant.java:1232)

          Issue Links

            Activity

            Hide
            dfuchs Daniel Fuchs added a comment -
            The following patch solves the issue:

            --- a/src/java.base/share/classes/java/time/Instant.java
            +++ b/src/java.base/share/classes/java/time/Instant.java
            @@ -1229,9 +1229,15 @@
                  * @throws ArithmeticException if numeric overflow occurs
                  */
                 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);
                     return millis + nanos / 1000_000;
                 }
            + }
             
                 //-----------------------------------------------------------------------
                 /**
            Show
            dfuchs Daniel Fuchs added a comment - The following patch solves the issue: --- a/src/java.base/share/classes/java/time/Instant.java +++ b/src/java.base/share/classes/java/time/Instant.java @@ -1229,9 +1229,15 @@       * @throws ArithmeticException if numeric overflow occurs       */      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);          return millis + nanos / 1000_000;      } + }        //-----------------------------------------------------------------------      /**
            Hide
            hgupdate HG Updates added a comment -
            URL: http://hg.openjdk.java.net/jdk9/dev/jdk/rev/7c6d6f1b7a56
            User: dfuchs
            Date: 2015-03-02 13:47:57 +0000
            Show
            hgupdate HG Updates added a comment - URL: http://hg.openjdk.java.net/jdk9/dev/jdk/rev/7c6d6f1b7a56 User: dfuchs Date: 2015-03-02 13:47:57 +0000
            Hide
            hgupdate HG Updates added a comment -
            URL: http://hg.openjdk.java.net/jdk9/jdk9/jdk/rev/7c6d6f1b7a56
            User: lana
            Date: 2015-03-11 02:21:44 +0000
            Show
            hgupdate HG Updates added a comment - URL: http://hg.openjdk.java.net/jdk9/jdk9/jdk/rev/7c6d6f1b7a56 User: lana Date: 2015-03-11 02:21:44 +0000

              People

              • Assignee:
                dfuchs Daniel Fuchs
                Reporter:
                dfuchs Daniel Fuchs
              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: