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

LazyBSONObject incorrectly decodes BSON regular expressions

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 2.12.0
    • Affects Version/s: 2.11.2
    • Component/s: BSON
    • None
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      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.

            Assignee:
            jeff.yemin@mongodb.com Jeffrey Yemin
            Reporter:
            daniel.gottlieb@mongodb.com Daniel Gottlieb (Inactive)
            None
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: