[JAVA-159] Issue with ObjectId.isValid method Created: 30/Aug/10  Updated: 29/Oct/10  Resolved: 28/Sep/10

Status: Closed
Project: Java Driver
Component/s: None
Affects Version/s: 2.0, 2.1
Fix Version/s: 2.2

Type: Bug Priority: Major - P3
Reporter: Shyamantak Assignee: Eliot Horowitz (Inactive)
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

All



 Description   

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.



 Comments   
Comment by Shyamantak [ 01/Oct/10 ]

Excellent!! Thanks a lot Eliot for letting know about it.

Generated at Thu Feb 08 08:51:37 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.