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

UUID:fromString() parses incorrect strings without error

    Details

    • Subcomponent:
    • CPU:
      generic
    • OS:
      generic

      Description

      FULL PRODUCT VERSION :
      java version "1.8.0_92"
      Java(TM) SE Runtime Environment (build 1.8.0_92-b14)
      Java HotSpot(TM) 64-Bit Server VM (build 25.92-b14, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      OS X 10.11.5

      A DESCRIPTION OF THE PROBLEM :
      UUID parsing is not working as expected. Additional leading digits in groups are swallowed without exceptions and the result is not the expected.

      4d4d8f3b-3b81-44f3-968d-d1c1a48b4ac8 is a vlid UUID.

      24d4d8f3b-3b81-44f3-968d-d1c1a48b4ac8 is not (added a '2' in front of the valid one)

      Calling UUID::fromString() with either one results in the same result, an UUID representing 4d4d8f3b-3b81-44f3-968d-d1c1a48b4ac8.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Just add a leading digit to a valid UUID and parse it

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      UUID::fromString() should throw IllegalArgumentException if passing a UUID that doesn't match the specification.
      ACTUAL -
      When parsing a UUID with an added leading digit, the original UUID is returned.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      String uuid = "4d4d8f3b-3b81-44f3-968d-d1c1a48b4ac8";

      assert UUID.fromString(uuid).toString().equals(uuid);

      assert !UUID.fromString("2" + uuid).toString().equals(uuid);
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      In the implementation of fromString() (pasted below), validate each group before building the result. At least the length of each string in components array should be checked, that is not expensive.

      public static UUID fromString(String name) {
              String[] components = name.split("-");
              if (components.length != 5)
                  throw new IllegalArgumentException("Invalid UUID string: "+name);
              for (int i=0; i<5; i++)
                  components[i] = "0x"+components[i];

              long mostSigBits = Long.decode(components[0]).longValue();
              mostSigBits <<= 16;
              mostSigBits |= Long.decode(components[1]).longValue();
              mostSigBits <<= 16;
              mostSigBits |= Long.decode(components[2]).longValue();

              long leastSigBits = Long.decode(components[3]).longValue();
              leastSigBits <<= 48;
              leastSigBits |= Long.decode(components[4]).longValue();

              return new UUID(mostSigBits, leastSigBits);
          }

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                rpatil Ramanand Patil
                Reporter:
                webbuggrp Webbug Group
              • Votes:
                0 Vote for this issue
                Watchers:
                4 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: