[JAVA-953] LazyBSONObject incorrectly decodes BSON regular expressions Created: 24/Sep/13  Updated: 03/Apr/14  Resolved: 30/Sep/13

Status: Closed
Project: Java Driver
Component/s: BSON
Affects Version/s: 2.11.2
Fix Version/s: 2.12.0

Type: Bug Priority: Major - P3
Reporter: Daniel Gottlieb (Inactive) Assignee: Jeffrey Yemin
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends

 Description   

Code to reproduce:

import java.util.*;
import java.util.regex.*;
import com.mongodb.*;
import org.bson.*;
 
public class BadPattern {
    public static void main(String[] jargs) throws Exception {
        Mongo mongo = new MongoClient();
        DBCollection coll = mongo.getDB("test").getCollection("patterns");
        coll.drop();
        List<Pattern> patterns = new ArrayList<Pattern>();
        patterns.add(Pattern.compile("pattern_string_1"));
        patterns.add(Pattern.compile("pattern_string_2"));
 
        coll.insert(new BasicDBObject("patterns", patterns));
 
        coll.setDBDecoderFactory(LazyDBDecoder.FACTORY);
        LazyDBObject obj = (LazyDBObject)coll.findOne();
        System.out.println(obj.get("patterns"));
    }
}

Output:

Exception in thread "main" java.lang.IllegalArgumentException: unrecognized flag [
                                                                                  ] 11
	at org.bson.BSON.regexFlags(BSON.java:94)
	at org.bson.LazyBSONObject.getElementValue(LazyBSONObject.java:597)
	at org.bson.LazyBSONList$LazyBSONListIterator.next(LazyBSONList.java:91)
	at com.mongodb.util.JSONSerializers$IterableSerializer.serialize(JSONSerializers.java:274)
	at com.mongodb.util.ClassMapBasedObjectSerializer.serialize(ClassMapBasedObjectSerializer.java:79)
	at com.mongodb.util.JSON.serialize(JSON.java:56)
	at com.mongodb.util.JSON.serialize(JSON.java:41)
	at org.bson.LazyDBList.toString(LazyDBList.java:29)
	at java.lang.String.valueOf(String.java:2854)
	at java.io.PrintStream.println(PrintStream.java:821)

Note I chose to create a list of two patterns. The problem is more visible as an IllegalArgumentException gets bubbled up. In the case of a single pattern followed by the end of the bson document results in an empty string being read in, thus no visible error. (I believe the null terminator being found is the end of the bson document).

In LazyBSONObject, the code to parse a pattern in `getElementValue` is:

            case BSON.REGEX:
                int patternCStringSize = sizeCString( record.valueOffset );
                String pattern = _input.getCString( record.valueOffset );
                String flags = _input.getCString( record.valueOffset + patternCStringSize + 1 );
                return Pattern.compile( pattern, BSON.regexFlags( flags ) );

I believe the `+ 1` part in parsing the flags should be removed.



 Comments   
Comment by Jeffrey Yemin [ 27/Feb/14 ]

Hi Dan,

With 2.12.0-rc0, the program in the description now outputs:

[ { "$regex" : "pattern_string_1"} , { "$regex" : "pattern_string_2"}]

If you'd like to do additional verification, 2.12.0-rc0 is available on Maven Central or the Github releases pages.

Comment by auto [ 30/Sep/13 ]

Author:

{u'username': u'jyemin', u'name': u'Jeff Yemin', u'email': u'jeff.yemin@10gen.com'}

Message: JAVA-953: LazyBSONObject incorrectly decodes BSON regular expressions
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/59e10f059d2862c26edfdf27565920b263640097

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