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

BigInteger/BigDecimal not immune to overflow, contrary to spec.

    Details

    • Type: CSR
    • Status: Closed
    • Priority: P4
    • Resolution: Approved
    • Fix Version/s: 11
    • Component/s: core-libs
    • Labels:
      None
    • Subcomponent:
    • Compatibility Risk:
      minimal
    • Interface Kind:
      Java API
    • Scope:
      SE

      Description

      Summary

      Clarify the specification of BigInteger to state its range is only "very large" as opposed to "infinite".

      Problem

      While a range on the order of 2^Integer.MAX_VALUE was once at the limit of what common computers could operate on, today that is only a very large as opposed to effectively infinite value. The specification of BigInteger should be adjusted accordingly.

      Solution

      Rephrase the specification of BigInteger to avoid implying it has infinite range and make the supported range requirements more prominent.

      Specification

      diff -r 2d1952d4d067 src/java.base/share/classes/java/math/BigInteger.java
      --- a/src/java.base/share/classes/java/math/BigInteger.java Tue May 08 09:51:42 2018 +0800
      +++ b/src/java.base/share/classes/java/math/BigInteger.java Tue May 08 09:32:57 2018 -0700
      @@ -52,19 +52,17 @@
        * and a few other miscellaneous operations.
        *
        * <p>Semantics of arithmetic operations exactly mimic those of Java's integer
      - * arithmetic operators, as defined in <i>The Java Language Specification</i>.
      + * arithmetic operators, as defined in <i>The Java&trade;  Language Specification</i>.
        * For example, division by zero throws an {@code ArithmeticException}, and
        * division of a negative by a positive yields a negative (or zero) remainder.
      - * All of the details in the Spec concerning overflow are ignored, as
      - * BigIntegers are made as large as necessary to accommodate the results of an
      - * operation.
        *
        * <p>Semantics of shift operations extend those of Java's shift operators
        * to allow for negative shift distances.  A right-shift with a negative
        * shift distance results in a left shift, and vice-versa.  The unsigned
      - * right shift operator ({@code >>>}) is omitted, as this operation makes
      - * little sense in combination with the "infinite word size" abstraction
      - * provided by this class.
      + * right shift operator ({@code >>>}) is omitted since this operation
      + * only makes sense for a fixed sized word and not for a
      + * representation conceptually having an infinite number of leading
      + * virtual sign bits.
        *
        * <p>Semantics of bitwise logical operations exactly mimic those of Java's
        * bitwise integer operators.  The binary operators ({@code and},
      @@ -84,8 +82,8 @@
        * extended so that it contains the designated bit.  None of the single-bit
        * operations can produce a BigInteger with a different sign from the
        * BigInteger being operated on, as they affect only a single bit, and the
      - * "infinite word size" abstraction provided by this class ensures that there
      - * are infinitely many "virtual sign bits" preceding each BigInteger.
      + * arbitrarily large abstraction provided by this class ensures that conceptually
      + * there are infinitely many "virtual sign bits" preceding each BigInteger.
        *
        * <p>For the sake of brevity and clarity, pseudo-code is used throughout the
        * descriptions of BigInteger methods.  The pseudo-code expression
      @@ -105,13 +103,18 @@
        * +2<sup>{@code Integer.MAX_VALUE}</sup> (exclusive)
        * and may support values outside of that range.
        *
      + * An {@code ArithmeticException} is thrown when a BigInteger
      + * constructor or method would generate a value outside of the
      + * supported range.
      + *
        * The range of probable prime values is limited and may be less than
        * the full supported positive range of {@code BigInteger}.
        * The range must be at least 1 to 2<sup>500000000</sup>.
        *
        * @implNote
      - * BigInteger constructors and operations throw {@code ArithmeticException} when
      - * the result is out of the supported range of
      + * In the reference implementation, BigInteger constructors and
      + * operations throw {@code ArithmeticException} when the result is out
      + * of the supported range of
        * -2<sup>{@code Integer.MAX_VALUE}</sup> (exclusive) to
        * +2<sup>{@code Integer.MAX_VALUE}</sup> (exclusive).
        *

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                darcy Joe Darcy
                Reporter:
                martin Martin Buchholz
                Reviewed By:
                Brian Burkhalter
              • Votes:
                0 Vote for this issue
                Watchers:
                1 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: