STL check: Check if someone tries to return std::string(crash_me).c_str()

This commit is contained in:
Thomas Jarosch 2011-10-26 21:45:27 +02:00
parent 84a763d3b0
commit c4dabd61e9
2 changed files with 9 additions and 0 deletions

View File

@ -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()) {

View File

@ -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"