[SERVER-9803] Handle regular expression parse errors without seg faulting Created: 29/May/13 Updated: 24/Oct/19 Resolved: 27/Jul/13 |
|
| Status: | Closed |
| Project: | Core Server |
| Component/s: | JavaScript, Shell |
| Affects Version/s: | None |
| Fix Version/s: | 2.5.2 |
| Type: | Bug | Priority: | Minor - P4 |
| Reporter: | Mihai Lacatusu | Assignee: | Tad Marshall |
| Resolution: | Done | Votes: | 0 |
| Labels: | regex, todo_in_code | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Environment: |
|
||
| Issue Links: |
|
||||||||
| Operating System: | ALL | ||||||||
| Steps To Reproduce: | root@testvm1:~# /opt/mongodb/bin/mongo , {"name":"foo"}, {"name":"bar"}]) }, ) , , Wed May 29 11:04:05.469 0x746df1 0x5dce32 0x7fee00e724f0 0x99d71c 0x7f3457 0x6ee4d3 0x6ee4c6 0x6f2b30 0x990293 0x98fc73 0x9ecc30 0x34df04206362 root@testvm1:~# |
||||||||
| Participants: | |||||||||
| Description |
|
Do not seg fault when there is an error in parsing/creating regular expressions in javascript. Old description |
| Comments |
| Comment by Tad Marshall [ 27/Jul/13 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
I can't find anything actually broken at this point (after Mathias' fixes). The original report no longer produces bad behavior, and I get the expected SyntaxError when I try to use invalid Regex options.
We don't return RegExp objects as RegExp objects, but that seems like a separate issue. We do seem to parse them correctly and catch SyntaxErrors properly.
I think that this issue should be resolved as Fixed and separate issues opened for other issues. We no longer segfault on invalid Regex expressions. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by auto [ 19/Jun/13 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Author: {u'username': u'RedBeard0531', u'name': u'Mathias Stearn', u'email': u'mathias@10gen.com'}Message: The main focus of this ticket is tightening up input validation in Other related tickets partially addressed: Conflicts: jstests/constructors.js | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by auto [ 17/Jun/13 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Author: {u'username': u'RedBeard0531', u'name': u'Mathias Stearn', u'email': u'mathias@10gen.com'}Message: The main focus of this ticket is tightening up input validation in Other related tickets partially addressed: | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Scott Hernandez (Inactive) [ 31/May/13 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
That javascript engine (V8), and many other client languages, have different regular expression engines. This is one of those cases where support exists on the server (in one way), but the client cannot parse/create it. The $regex+$options separates things in a consistent way so each language can handle it explicitly but when you use the match group/option syntax it does not do this and can create "bad" regex strings (mixing options with the regular expression) that many langauges/clients cannot parse or which invalid. The reason this fails in the shell is that the server sends back a regular expression object with the "(?i)" in the front just like you had in the explain output. This fails on the client (V8 javascript) when parsing the regex to display the explain results, and might fail on other clients/languages as well. But constructing the regular expression object in the native language (using RegExp(<expr>, <opts>) or /<expr>/<opts>) will produce the correct separation of options from expression, and not cause problems. The goal of this issue to keep the client from seg faulting when there is an error in parsing/creating regular expressions. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Mihai Lacatusu [ 31/May/13 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Thanks I'll take your suggestion on changing the regex. However I wouldn't say the regexp is "bad" because MongoDB documentation does state that "$regex uses PCRE as the matching engine", so (?i) syntax should be valid. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Scott Hernandez (Inactive) [ 30/May/13 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Also note that the correct $regex format for your query is {$regex:"^mih", $options:"i"} as defined here: http://docs.mongodb.org/manual/reference/operator/regex/ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Tad Marshall [ 30/May/13 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Hi Mihai, Thanks for the report! I reproduced your crash in the current master branch (testing a debug version on Windows in the debugger). mongo shell:
Program call stack from debugger:
The fatal error is thrown by V8 because we are trying to use the Set API function to associate a zero V8 handle to store data for a BSON object. Our code has called mongoToV8Element(subElem, readOnly) and this function has returned a NULL handle, but we do not check for this before calling V8 to store it. It seems that the flavor of regular expression provided by V8 does not support the expression you are providing.
This is a shortcoming of V8 in the version that we are using. The bug in MongoDB is that the code assumes that an attempt to construct a RegExp value will succeed and return a valid handle. This regular expression (using the mode modifier "(?i)" for case insensitivity) does not succeed in producing a V8 object, and when the code tries to store the NULL handle V8 throws a fatal error, aborting the shell. The code that is trying to store the NULL value in this case is in src/mongo/scripting/engine_v8.cpp in lines 1389 to 1394:
Tad | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Scott Hernandez (Inactive) [ 29/May/13 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
I also found that when using the regex object in javascript I did not get the error, so you can do that as well. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Mihai Lacatusu [ 29/May/13 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
I'll run the explains from python to workaround this one - thanks Scott. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Scott Hernandez (Inactive) [ 29/May/13 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
You will want to use the python RegexObject type: http://docs.python.org/2/library/re.html#re.RegexObject You can also use the python shell to do the explain if you want to use the same query or code and do it in python. http://api.mongodb.org/python/current/api/pymongo/cursor.html#pymongo.cursor.Cursor.explain | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Mihai Lacatusu [ 29/May/13 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Thanks Scott. I confirm, this does not affect the server, but only the shell client. Sorry if this bug report was placed in the wrong module. I was running the query using the $regex wrapper because I was profiling the exact same queries executed by our Python code. It seems that the /<regex>/<options> syntax doesn't work through pymongo. So we're using $regex wrapper and the goal of my profiling was to make sure that this query makes use of an index. Are these 2 syntaxes 100% equivalent at MongoDB query engine level? How can I get the explain() output for the $regex syntax? Thanks in advance! | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Scott Hernandez (Inactive) [ 29/May/13 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
The seg fault is just for the mongo process, not the server process as far as I can see. Please confirm that this is not affecting the server, but just the client. Also, in the shell I think you want this query using the javascript regex object (/<regex>/<options>), and not the special $regex wrapper:
Are you storing any "name" values which are regular expressions themselves? If not you def. don't want the syntax you are using. |