Uninitialized vars: handle printf a little better. Ticket: #3050

This commit is contained in:
Daniel Marjamäki 2011-09-05 20:18:58 +02:00
parent c7d0beefa8
commit 2601733ff7
2 changed files with 15 additions and 0 deletions

View File

@ -118,6 +118,15 @@ void CheckNullPointer::parseFunctionCall(const Token &tok, std::list<const Token
if (functionNames2.find(tok.str()) != functionNames2.end())
var.push_back(tok.tokAt(4));
}
// TODO: Handle sprintf/printf better.
if (Token::Match(&tok, "printf ( %str% , %var% ,|)") && tok.tokAt(4)->varId() > 0)
{
const std::string &formatstr(tok.tokAt(2)->str());
const std::string::size_type pos = formatstr.find("%");
if (pos != std::string::npos && formatstr.compare(pos,2,"%s") == 0)
var.push_back(tok.tokAt(4));
}
}

View File

@ -1075,6 +1075,12 @@ private:
" char *b = (a+2) & 7;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
checkUninitVar("void f() {\n" // Ticket #3050
" char a[2];\n"
" printf(\"%s\", a);\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: a\n", errout.str());
}
// alloc..