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

Result of BigDecimal.toString throws overflow exception on new BigDecimal(str)

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 19
    • 11, 14, 18
    • core-libs
    • b26
    • generic
    • generic
    • Verified

      A DESCRIPTION OF THE PROBLEM :
      Based upon the javadocs for BigDecimal, the lower bound for scale is -Integer.MAX_VALUE

      Quote:
      "The exponent consists of the character 'e' ('\u0065') or 'E' ('\u0045') followed by one or more decimal digits. The value of the exponent must lie between -Integer.MAX_VALUE (Integer.MIN_VALUE+1) and Integer.MAX_VALUE, inclusive."

      Ref: https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/math/BigDecimal.html#%3Cinit%3E(java.lang.String)

      If you do a .toString() on this value, I would expect to be able to deserialize the result using `new java.math.BigDecimal(str)`

      java.math.BigDecimal.valueOf(Long.MIN_VALUE, -Integer.MAX_VALUE).toString => "-9.223372036854775808E+2147483665"

      new java.math.BigDecimal("-9.223372036854775808E+2147483665") => Overflow Exception (because 2147483665 > Integer.MAX_VALUE of 2147483647) - and there is an (incorrect?) guard in place throwing the exception.

      Via experimentation, the actual lower bound is `-Integer.MAX_VALUE+18` or `Integer.MIN_VALUE+19`:

      java.math.BigDecimal.valueOf(Long.MIN_VALUE, -Integer.MAX_VALUE+18).toString = "-9.223372036854775808E+2147483647"

      new java.math.BigDecimal("-9.223372036854775808E+2147483647").toString() => "-9.223372036854775808E+2147483647"


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      import java.math.BigDecimal;

      String str = java.math.BigDecimal.valueOf(Long.MIN_VALUE, -Integer.MAX_VALUE);
      BigDecimal bd = new BigDecimal(str);


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      It should not throw an exception.

      FREQUENCY : always


            rgiulietti Raffaello Giulietti
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            8 Start watching this issue

              Created:
              Updated:
              Resolved: