Fixed #7293 (Use of uninitialized pointer not detected (worked in 1.71))

This commit is contained in:
Daniel Marjamäki 2017-04-24 18:27:16 +02:00
parent 6b685f5a80
commit 37fd60e879
3 changed files with 18 additions and 10 deletions

View File

@ -2849,16 +2849,18 @@ static void valueFlowUninit(TokenList *tokenlist, SymbolDatabase * /*symbolDatab
continue; continue;
const Token *vardecl = tok->next(); const Token *vardecl = tok->next();
bool stdtype = false; bool stdtype = false;
bool pointer = false;
while (Token::Match(vardecl, "%name%|::|*") && vardecl->varId() == 0) { while (Token::Match(vardecl, "%name%|::|*") && vardecl->varId() == 0) {
stdtype |= vardecl->isStandardType(); stdtype |= vardecl->isStandardType();
pointer |= vardecl->str() == "*";
vardecl = vardecl->next(); vardecl = vardecl->next();
} }
if (!tokenlist->isC() && !stdtype && !pointer)
continue;
if (!Token::Match(vardecl, "%var% ;")) if (!Token::Match(vardecl, "%var% ;"))
continue; continue;
if (Token::Match(vardecl, "%varid% ; %varid% =", vardecl->varId())) if (Token::Match(vardecl, "%varid% ; %varid% =", vardecl->varId()))
continue; continue;
if (!tokenlist->isC() && !stdtype)
continue;
const Variable *var = vardecl->variable(); const Variable *var = vardecl->variable();
if (!var || var->nameToken() != vardecl) if (!var || var->nameToken() != vardecl)
continue; continue;

View File

@ -441,7 +441,7 @@ private:
// #2641 - local pointer, function call // #2641 - local pointer, function call
check("void f() {\n" check("void f() {\n"
" ABC *abc;\n" " ABC *abc = abc1;\n"
" abc->a = 0;\n" " abc->a = 0;\n"
" do_stuff();\n" " do_stuff();\n"
" if (abc) { }\n" " if (abc) { }\n"

View File

@ -2377,16 +2377,22 @@ private:
} }
void valueFlowUninit() { void valueFlowUninit() {
const char* code;
std::list<ValueFlow::Value> values; std::list<ValueFlow::Value> values;
const char* code = "void f() {\n" code = "void f() {\n"
" int x;\n" " int x;\n"
" switch (x) {}\n" " switch (x) {}\n"
"}"; "}";
values = tokenValues(code, "x )"); values = tokenValues(code, "x )");
ASSERT_EQUALS(1U, values.size()); ASSERT_EQUALS(true, values.size()==1U && values.front().isUninitValue());
ASSERT_EQUALS(ValueFlow::Value::UNINIT, values.empty() ? 0 : values.front().valueType);
code = "void f() {\n"
" C *c;\n"
" if (c->x() == 4) {}\n"
"}";
values = tokenValues(code, "c .");
ASSERT_EQUALS(true, values.size()==1U && values.front().isUninitValue());
} }
}; };