[SERVER-21740] Shell can't print invalid or native functions stored in a document Created: 02/Dec/15  Updated: 14/Apr/16  Resolved: 09/Mar/16

Status: Closed
Project: Core Server
Component/s: JavaScript, Shell
Affects Version/s: 2.6.11, 3.0.7, 3.2.0-rc5
Fix Version/s: None

Type: Bug Priority: Trivial - P5
Reporter: Robert Guo (Inactive) Assignee: Robert Guo (Inactive)
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to SERVER-9131 Ensure documents with code elements d... Closed
Backwards Compatibility: Fully Compatible
Operating System: ALL
Steps To Reproduce:

db.test.insert({a:print})
db.test.find()

Sprint: TIG 11 (03/11/16)
Participants:

 Description   

attempting to store and find an invalid or native function from the shell gives a SyntaxError.

It looks like the cause is that the shell attempts to eval a function in a document, which would fail if the function is invalid.

2015-12-02T15:04:39.330-0500 E QUERY    [thread1] SyntaxError: missing ] after element list :
tojsonObject@src/mongo/shell/types.js:701:13
tojson@src/mongo/shell/types.js:647:17
DBQuery.prototype.shellPrint@src/mongo/shell/query.js:522:65
shellPrintHelper@src/mongo/shell/utils.js:462:1
@(shell2):1:1



 Comments   
Comment by Robert Guo (Inactive) [ 09/Mar/16 ]

fixed as a result of SERVER-9131

Comment by Kevin Pulo [ 03/Mar/16 ]

Looks like this can be resolved as either Duplicate or Gone Away, courtesy of SERVER-9131.

$ /m/3.3.2/bin/mongo --norc --enableJavaScriptProtection
MongoDB shell version: 3.3.2
connecting to: test
> db.test.drop()
true
> db.test.insert({a:print})
Cannot use 'commands' readMode, degrading to 'legacy' mode
WriteResult({ "nInserted" : 1 })
> db.test.find()
{ "_id" : ObjectId("56d8be28e7e1b4217fd08085"), "a" : { "code" : "function print() {\n    [native code]\n}" } }

Comment by Robert Guo (Inactive) [ 02/Dec/15 ]

max.hirschhorn pointed out the root cause is that the code is being eval'd here automatically. https://github.com/mongodb/mongo/blob/r3.2.0-rc5/src/mongo/scripting/mozjs/valuereader.cpp#L55-L56

and the error message is generate here: https://github.com/mongodb/mongo/blob/r3.2.0-rc5/src/mongo/scripting/mozjs/implscope.cpp#L769

There would be a similar error message on the server if we wrapped the find in a db.eval

2015-12-02T16:21:53.165-0500 E QUERY    [js] SyntaxError: missing ] after element list :
tojsonObject@src/mongo/shell/types.js:701:13
tojson@src/mongo/shell/types.js:647:17
Array.tojson@src/mongo/shell/types.js:182:23
tojsonObject@src/mongo/shell/types.js:676:1
tojson@src/mongo/shell/types.js:647:17
printjson@src/mongo/shell/types.js:726:12
_funcs1@:1:31

One possible fix for this would be to add a Code data type to the shell.

Generated at Thu Feb 08 03:58:16 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.