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

java.util.UUID.fromString accepts input that does not match expected format

XMLWordPrintable

      ADDITIONAL SYSTEM INFORMATION :
      Mac OS X 10.13.6, Java 10.0.1, as well as 1.8

      A DESCRIPTION OF THE PROBLEM :
      The UUID.fromString javadoc states that the input is parsed according to the format specified in UUID.toString, and will throw IllegalArgumentException if the input does not conform to that format. However, the fromString method accepts a more liberal set of input values than is described in the toString BNF.

      Either the javadoc is misleading about what is allowed, or the implementation is incorrect in what it accepts.

      Example (jshell 10.0.1):

      jshell> UUID.fromString("1-1-1-1-1")
      $2 ==> 00000001-0001-0001-0001-000000000001

      Yet the input format does not match what is described in the toString doc.

      Also, here is another example where the input is accepted perversely:

      jshell> UUID.fromString("5-4-3-DEADBEEF0002-9000000001")
      $3 ==> 00000005-0004-0003-0002-009000000001

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      UUID.fromString("5-4-3-DEADBEEF0002-9000000001")

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      an IllegalArgumentException should be thrown
      ACTUAL -
      UUID object '00000005-0004-0003-0002-009000000001' is returned

      ---------- BEGIN SOURCE ----------
      import java.util.UUID;

      class Test{

      public static void main(String[] args) throws Exception{
      String input="5-4-3-DEADBEEF0002-9000000001";
      System.out.println("Calling UUID.fromString with input: "+input);
      try{
      UUID uuid=UUID.fromString(input);
      throw new Exception("Expected IllegalArgumentException exception but got: "+uuid );
      }catch(IllegalArgumentException e){
      System.out.println("OK: caught exception: "+e);
      }
      }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      The only workaround is to manually validate UUID format before calling UUID.fromString

            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: