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

BigDecimal#stripTrailingZeros can throw undocumented ArithmeticException

    XMLWordPrintable

Details

    • b16
    • Verified

    Description

      A DESCRIPTION OF THE PROBLEM :
      BigDecimal#stripTrailingZeros can throw an ArithmeticException when the scale overflows, but is not documented to do so.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Execute BigDecimal.valueOf(10, -2147483648).stripTrailingZeros(); (see source code below for a complete example)

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      No exception thrown as BigDecimal#stripTrailingZeros() is documented to not throw any exceptions (see https://github.com/openjdk/jdk/blob/45e1bab87ccebd498d4f927a008dd89a47dd177b/src/java.base/share/classes/java/math/BigDecimal.java#L3080).
      ACTUAL -
      Exception in thread "main" java.lang.ArithmeticException: Overflow
      at java.base/java.math.BigDecimal.checkScale(BigDecimal.java:4426)
      at java.base/java.math.BigDecimal.createAndStripZerosToMatchScale(BigDecimal.java:4872)
      at java.base/java.math.BigDecimal.stripTrailingZeros(BigDecimal.java:3034)
      at StripTrailingZeroes.main(StripTrailingZeros.java:5)

      ---------- BEGIN SOURCE ----------
      import java.math.BigDecimal;
        
      public class StripTrailingZeros {
          public static void main(String[] args) {
              BigDecimal.valueOf(10, -2147483648).stripTrailingZeros();
          }
      }
      ---------- END SOURCE ----------

      Attachments

        Issue Links

          Activity

            People

              darcy Joe Darcy
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              6 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: