[SERVER-4007] The edit() function (command) in shell/dbShell.cpp could index off the end of a string Created: 04/Oct/11  Updated: 11/Jul/16  Resolved: 15/Oct/11

Status: Closed
Project: Core Server
Component/s: Shell
Affects Version/s: None
Fix Version/s: 2.1.0

Type: Bug Priority: Minor - P4
Reporter: Tad Marshall Assignee: Eliot Horowitz (Inactive)
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Any


Operating System: ALL
Participants:

 Description   

The code in the new 'edit' command in the shell uses the .data() member of <basic_string> (i.e. <string>) but then tests for NUL to end the string. The code should use .c_str() instead, which does NUL terminate. This is probably not a real-world error because real implementations of <string> seem to store strings NUL-terminated anyway.

#ifndef _WIN32
static void edit(const string& var){
static const char * editor = getenv("EDITOR");
if (!editor)

{ cout << "please define the EDITOR environment variable" << endl; return; }

for (const char* p=var.data(); *p ; p++){ // should use c_str() instead of data()
if (! (isalnum(*p) || *p == '_' || *p == '.'))

{ cout << "can only edit variable or property" << endl; return; }

}



 Comments   
Comment by auto [ 15/Oct/11 ]

Author:

{u'login': u'erh', u'name': u'Eliot', u'email': u'eliot@10gen.com'}

Message: Merge pull request #114 from tadmarshall/SERVER-4007-reissue

SERVER-4007 (reissue) – use .c_str() instead of .data()
Branch: master
https://github.com/mongodb/mongo/commit/feabf3ca0a5487e5950112fa09858d7e87b50975

Comment by auto [ 15/Oct/11 ]

Author:

{u'login': u'erh', u'name': u'Eliot', u'email': u'eliot@10gen.com'}

Message: Merge pull request #114 from tadmarshall/SERVER-4007-reissue

SERVER-4007 (reissue) – use .c_str() instead of .data()
Branch: master
https://github.com/mongodb/mongo/commit/feabf3ca0a5487e5950112fa09858d7e87b50975

Comment by Tad Marshall [ 13/Oct/11 ]

I closed pull request 101 and opened https://github.com/mongodb/mongo/pull/114 to replace it.
Same fix, but without the unrelated commits.

Comment by Tad Marshall [ 06/Oct/11 ]

Pull request https://github.com/mongodb/mongo/pull/101 addresses this.

Comment by Tad Marshall [ 04/Oct/11 ]

A question on stackoverflow addresses the difference between std::string.data() and std::string.c_str(). In the responses it is shown that MSVC (at least) internally calls c_str() from data() and that the new C++11 standard requires this behavior. So the ISO 2003 TR1 spec hasn't matched reality for while.

http://stackoverflow.com/questions/1534399/whats-the-difference-between-stdstringc-str-and-stdstringdata

So, it's only a bug in the technical sense, no user impact.

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