Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-159

Issue with ObjectId.isValid method

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 2.2
    • Affects Version/s: 2.0, 2.1
    • Component/s: None
    • None
    • Environment:
      All

      if you see the ObjectId.java (http://github.com/mongodb/mongo-java-
      driver/blob/master/src/main/org/bson/types/ObjectId.java)

      isValid(String) method it returns "true" for those strings whose
      character length is between 18 and 24 and whose each character happens
      to be a qualified hex character.

      And then if you see the constructor code, you would notice following
      piece of code:

      byte b[] = new byte[12];
      for ( int i=0; i<b.length; i++ )

      { b[b.length-(i+1)] = (byte)Integer.parseInt( s.substring( i*2 , i*2 + 2) , 16 ); }

      Here it expects the string to be of 24 characters.

      Hence sometimes though isValid method returns true it cannot create a
      valid ObjectId. It throws java.lang.StringIndexOutOfBoundsException:
      String index out of range.

      This is an important issue as in some cases does not allow to store valid data.

            Assignee:
            eliot Eliot Horowitz (Inactive)
            Reporter:
            mrgautamsam Shyamantak
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: