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

BigDecimal.fractionOnly() erroneously returns true for large scale value

XMLWordPrintable

    • b17
    • generic
    • generic
    • Verified

      A DESCRIPTION OF THE PROBLEM :
      The internal method java.math.BigDecimal.fractionOnly() erroneously returns true for large scale values because it calculates `int - int` which can overflow; instead it should first cast one operand to `long`.

      This leads to an incorrect exception message for java.math.BigDecimal.longValueExact() which claims "Rounding necessary" while the message should be "Overflow".

      IMPORTANT: If you fix this numeric overflow in BigDecimal.fractionOnly(), you must also fix it here: https://github.com/openjdk/jdk/blob/d8158897c3d0dbea46e4f55ad8b501252d88b7e1/src/java.base/share/classes/java/math/BigDecimal.java#L3628 (same numeric overflow problem for large `scale` values)
      Otherwise this might circumvent the fix for JDK-8211936.


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the following two statements and inspect the exception messages:
      new BigDecimal("1e" + (Integer.MAX_VALUE - 1)).longValueExact()
      new BigDecimal("1e" + Integer.MAX_VALUE).longValueExact()


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      java.lang.ArithmeticException: Overflow
      java.lang.ArithmeticException: Overflow

      ACTUAL -
      java.lang.ArithmeticException: Overflow
      java.lang.ArithmeticException: Rounding necessary


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

              Created:
              Updated:
              Resolved: