Fixed #4183 (false positive with method named c_str())

This commit is contained in:
Daniel Marjamäki 2012-11-28 08:48:48 +01:00
parent fe8b6f0f95
commit 40719c56db
2 changed files with 14 additions and 2 deletions

View File

@ -1194,8 +1194,11 @@ void CheckStl::string_c_str()
tok2 = tok->next()->link(); tok2 = tok->next()->link();
else else
tok2 = tok2->previous(); tok2 = tok2->previous();
if (tok2 && Token::simpleMatch(tok2->tokAt(-4), ". c_str ( )")) if (tok2 && Token::simpleMatch(tok2->tokAt(-4), ". c_str ( )")) {
string_c_strParam(tok, i->second); const Variable* var = symbolDatabase->getVariableFromVarId(tok2->tokAt(-5)->varId());
if (var && Token::Match(var->typeStartToken(), "const| std :: string|wstring"))
string_c_strParam(tok, i->second);
}
} }
} }

View File

@ -1864,6 +1864,15 @@ private:
" return hello().c_str();\n" " return hello().c_str();\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:11]: (error) Dangerous usage of c_str(). The value returned by c_str() is invalid after this call.\n", errout.str()); ASSERT_EQUALS("[test.cpp:11]: (error) Dangerous usage of c_str(). The value returned by c_str() is invalid after this call.\n", errout.str());
// #4183 - using MyStringClass.c_str()
check("void a(const std::string &str);\n"
"\n"
"void b() {\n"
" MyStringClass s;\n"
" a(s.c_str());\n"
"}");
ASSERT_EQUALS("", errout.str());
} }
void autoPointer() { void autoPointer() {