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

Enhance robustness of some j.m.BigInteger constructors

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Withdrawn
    • Icon: P4 P4
    • tbd
    • core-libs
    • None
    • behavioral
    • low
    • Hide
      Currently, an invocation like `new BigInteger(new byte[] {1, 2, ...}, 0, 0)` returns an instance representing 0, while `new BigInteger(new byte[] {}, 0, 0)` throws a `NumberFormatException`.

      With this change, both invocations throw a `NumberFormatException`.

      It would be rare to specify an empty range of indices. But if so, the behavior should be the same, regardless of the array being empty or not.
      Show
      Currently, an invocation like `new BigInteger(new byte[] {1, 2, ...}, 0, 0)` returns an instance representing 0, while `new BigInteger(new byte[] {}, 0, 0)` throws a `NumberFormatException`. With this change, both invocations throw a `NumberFormatException`. It would be rare to specify an empty range of indices. But if so, the behavior should be the same, regardless of the array being empty or not.
    • Java API
    • SE

      Summary

      Extend the @throws NumberFormatException clause of constructor BigInteger(byte[],int,int)

      Problem

      The current @throws NumberFormatException clause of constructor BigInteger(byte[],int,int) only refers to the length of the val array. Since the proposed code also throws the same exception when len is zero, this must be reflected in the clause as well.

      Solution

      Add text for the zero len case to the clause.

      Specification

      --- a/src/java.base/share/classes/java/math/BigInteger.java
      +++ b/src/java.base/share/classes/java/math/BigInteger.java
      @@ -340,23 +340,25 @@ public class BigInteger extends Number implements Comparable<BigInteger> {
            *         two's-complement binary representation of a BigInteger.
            * @param  off the start offset of the binary representation.
            * @param  len the number of bytes to use.
      -     * @throws NumberFormatException {@code val} is zero bytes long.
      +     * @throws NumberFormatException {@code val} is zero bytes long,
      +     *         or {@code len} is zero.
            * @throws IndexOutOfBoundsException if the provided array offset and
            *         length would cause an index into the byte array to be
            *         negative or greater than or equal to the array length.
            * @since 9
            */
           public BigInteger(byte[] val, int off, int len) {

            rgiulietti Raffaello Giulietti
            rgiulietti Raffaello Giulietti
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: