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

Long.parseUnsignedLong should throw exception on too large input

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P3
    • Resolution: Fixed
    • Affects Version/s: 8
    • Fix Version/s: 9
    • Component/s: core-libs
    • Labels:
      None
    • Subcomponent:
    • Introduced In Version:
      8
    • Resolved In Build:
      b02
    • Verification:
      Verified

      Description

      The Javadoc of Long.parseUnsignedLong specifies that it should throw a
      NumberFormatException if "the value represented by the string is larger
      than the largest unsigned long, 2^64-1."

      Test:

      import java.math.BigInteger;

      public class UnsignedLongBug {
        public static void main(String[] args) {
          try {
            String input = "1234567890abcdef1";
            System.out.println(Long.parseUnsignedLong(input, 16));
            BigInteger maxUnsignedLong =
      BigInteger.ONE.shiftLeft(64).subtract(BigInteger.ONE);
            BigInteger inputValue = new BigInteger(input, 16);
            System.out.println(maxUnsignedLong.compareTo(inputValue));
            throw new AssertionError();
          } catch (NumberFormatException expected) {
            System.out.println("Correct");
          }
        }
      }


      Found by: Louis Wasserman <lowasser@google.com>

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              bpb Brian Burkhalter
              Reporter:
              rriggs Roger Riggs
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: