Fixed #7656 (stlcstr - false positive)

This commit is contained in:
Daniel Marjamäki 2017-02-26 17:25:32 +01:00
parent f68fa72095
commit d840005f06
2 changed files with 13 additions and 3 deletions

View File

@ -1100,17 +1100,17 @@ void CheckStl::string_c_str()
} }
bool local = false; bool local = false;
bool ptr = false; bool ptrOrRef = false;
const Variable* lastVar = nullptr; const Variable* lastVar = nullptr;
const Function* lastFunc = nullptr; const Function* lastFunc = nullptr;
bool funcStr = false; bool funcStr = false;
if (Token::Match(tok2, "%var% .")) { if (Token::Match(tok2, "%var% .")) {
local = isLocal(tok2); local = isLocal(tok2);
ptr = tok2->variable() && tok2->variable()->isPointer(); ptrOrRef = tok2->variable() && (tok2->variable()->isPointer() || tok2->variable()->isReference());
} }
while (tok2) { while (tok2) {
if (Token::Match(tok2, "%var% .|::")) { if (Token::Match(tok2, "%var% .|::")) {
if (ptr) if (ptrOrRef)
local = false; local = false;
lastVar = tok2->variable(); lastVar = tok2->variable();
tok2 = tok2->tokAt(2); tok2 = tok2->tokAt(2);

View File

@ -2365,6 +2365,16 @@ private:
" return mapInfo.author.c_str();\n" " return mapInfo.author.c_str();\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:6]: (error) Dangerous usage of c_str(). The value returned by c_str() is invalid after this call.\n", errout.str()); ASSERT_EQUALS("[test.cpp:6]: (error) Dangerous usage of c_str(). The value returned by c_str() is invalid after this call.\n", errout.str());
check("struct S {\n" // #7656
" std::string data;\n"
"};\n"
"const S& getS();\n"
"const char* test() {\n"
" const struct S &s = getS();\n"
" return s.data.c_str();\n"
"}");
ASSERT_EQUALS("", errout.str());
} }
void autoPointer() { void autoPointer() {