STL check: Check if someone tries to return std::string(crash_me).c_str()
This commit is contained in:
parent
84a763d3b0
commit
c4dabd61e9
|
@ -1027,6 +1027,9 @@ void CheckStl::string_c_str()
|
|||
tok->next()->varId() > 0 &&
|
||||
localvar.find(tok->next()->varId()) != localvar.end()) {
|
||||
string_c_strError(tok);
|
||||
} else if (Token::simpleMatch(tok, "return std :: string (") &&
|
||||
Token::Match(tok->tokAt(4)->link(), ") . c_str ( ) ;")) {
|
||||
string_c_strError(tok);
|
||||
} else if (Token::Match(tok, "[;{}] %var% = %var% . str ( ) . c_str ( ) ;") &&
|
||||
tok->next()->varId() > 0 &&
|
||||
pointers.find(tok->next()->varId()) != pointers.end()) {
|
||||
|
|
|
@ -1323,6 +1323,12 @@ private:
|
|||
"}");
|
||||
ASSERT_EQUALS("[test.cpp:3]: (error) Dangerous usage of c_str()\n", errout.str());
|
||||
|
||||
check("const char *get_msg() {\n"
|
||||
" std::string errmsg;\n"
|
||||
" return std::string(\"ERROR: \" + errmsg).c_str();\n"
|
||||
"}");
|
||||
ASSERT_EQUALS("[test.cpp:3]: (error) Dangerous usage of c_str()\n", errout.str());
|
||||
|
||||
check("void f() {\n"
|
||||
" std::ostringstream errmsg;\n"
|
||||
" const char *c = errmsg.str().c_str();\n"
|
||||
|
|
Loading…
Reference in New Issue