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.