[SERVER-32355] Use of options:x and comments with $regex search including \n can lead to incorrect documents being returned Created: 14/Dec/17  Updated: 14/Dec/17  Resolved: 14/Dec/17

Status: Closed
Project: Core Server
Component/s: Querying
Affects Version/s: 3.4.9
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: William Byrne III Assignee: Unassigned
Resolution: Duplicate Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: File regexOptionsX.js    
Issue Links:
Duplicate
duplicates SERVER-32356 Use of options:x and comments with $r... Closed
Operating System: ALL
Steps To Reproduce:

Pass the following in a file to the mongo shell:

  db.regexOptionsX.drop();
 
  db.regexOptionsX.insert([
    {_id: 1, f1: "realapp"  },
    {_id: 2, f1: "rea"      },
    {_id: 3, f1: "rea\napp" },
    {_id: 4, f1: "rea\nlapp" },
    {_id: 5, f1: "suretrea" }
  ]) ;
 
  print("\nAll: find()");
  var a = db.regexOptionsX.find() ; while (a.hasNext()) {printjson(a.next())}
 
  print("\nQuotes: find({f1:{$regex:'rea \\n# comment 1\\nl',$options:'x'}})");
  var s = db.regexOptionsX.find({f1:{$regex:'rea \n# comment 1\nl',$options:'x'}})
  while (s.hasNext()) {printjson(s.next())}
 
  print("\nSlashes: find({f1:{$regex:/rea \\n# comment 1\\nl/,$options:'x'}})");
  var x = db.regexOptionsX.find({f1:{$regex:/rea \n# comment 1\nl/,$options:'x'}})
  while (x.hasNext()) {printjson(x.next())}
 
 
  print("\nNo Comment and Quotes: find({f1:{$regex:'rea \\nl',$options:'x'}})");
  var s = db.regexOptionsX.find({f1:{$regex:'rea \nl',$options:'x'}})
  while (s.hasNext()) {printjson(s.next())}
 
  print("\nNo Comment and Slashes: find({f1:{$regex:/rea \\nl/,$options:'x'}})");
  var x = db.regexOptionsX.find({f1:{$regex:/rea \nl/,$options:'x'}})
  while (x.hasNext()) {printjson(x.next())}
 
 
  print("\nNo space, no comment, and Quotes: find({f1:{$regex:'rea\\nl',$options:'x'}})");
  var s = db.regexOptionsX.find({f1:{$regex:'rea\nl',$options:'x'}})
  while (s.hasNext()) {printjson(s.next())}
 
  print("\nNo space, no comment, and Slashes: find({f1:{$regex:/rea\\nl/,$options:'x'}})");
  var x = db.regexOptionsX.find({f1:{$regex:/rea\nl/,$options:'x'}})
  while (x.hasNext()) {printjson(x.next())}
 
 
  print("\nNo options:x (or spaces or comments) and Quotes: find({f1:{$regex:'rea\\nl'}})");
  var s = db.regexOptionsX.find({f1:{$regex:'rea\nl'}})
  while (s.hasNext()) {printjson(s.next())}
 
  print("\nNo options:x (or spaces or comments) and Slashes: find({f1:{$regex:/rea\\nl/}})");
  var x = db.regexOptionsX.find({f1:{$regex:/rea\nl/}})
  while (x.hasNext()) {printjson(x.next())}

Summary of output:

  • The only document that should be returned is _ {"_id": 4, "f1": "rea\nlapp"}

    _ - it has the "rea", the newline and the "l".

  • If the $regex uses quotes and $options:'x', then this document is returned: _ { "_id" : 1, "f1" : "realapp"}

    _, meaning the newline is being seen as a formatting "space" and not to be searched for

  • If the $regex uses slashes, $options:'x', and has a comment, then this document: _ {"_id": 3, "f1": "rea\napp"}

    _ is returned as well as the correct one. With no comment it gets the right results, even with $options:'x' present.

Participants:

 Description   

With a simple set of documents:

  • {_id: 1, f1: "realapp"}
  • {_id: 2, f1: "rea"}
  • {_id: 3, f1: "rea\napp"}
  • {_id: 4, f1: "rea\nlapp"}
  • {_id: 5, f1: "suretrea"}

these two $regex expressions (identical except for use of quotes or slashes):

  • find({f1:{$regex:'rea \n# comment 1\nl',$options:"x"}})
  • find({f1:{$regex:/rea \n# comment 1\nl/,$options:"x"}})

give different results, both incorrect, despite our $regex documentation suggesting that the syntaxes with the search strings wrapped in quotes or slashes are equivalent.

Removing the option:x, the comment and the space leads to correct results from both forms.



 Comments   
Comment by Mark Agarunov [ 14/Dec/17 ]

Hello william.byrne,

As this appears to be the same issue detailed in SERVER-32356, I've closed this ticket as a duplicate.

Thanks,
Mark

Generated at Thu Feb 08 04:29:59 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.