CheckAutoVariables: use ValueFlow to detect more errors when pointer aliases are used

This commit is contained in:
Daniel Marjamäki 2015-11-15 12:10:35 +01:00
parent 573edb4c92
commit c10a10c26f
2 changed files with 21 additions and 1 deletions

View File

@ -88,9 +88,21 @@ bool CheckAutoVariables::isAutoVar(const Token *tok)
bool CheckAutoVariables::isAutoVarArray(const Token *tok)
{
// Variable
const Variable *var = tok->variable();
if (var && var->isLocal() && !var->isStatic() && var->isArray() && !var->isPointer())
return true;
return (var && var->isLocal() && !var->isStatic() && var->isArray() && !var->isPointer());
// ValueFlow
if (var && var->isPointer()) {
for (std::list<ValueFlow::Value>::const_iterator it = tok->values.begin(); it != tok->values.end(); ++it) {
const ValueFlow::Value &val = *it;
if (val.tokvalue && isAutoVarArray(val.tokvalue))
return true;
}
}
return false;
}
// Verification that we really take the address of a local variable

View File

@ -634,6 +634,14 @@ private:
"}");
ASSERT_EQUALS("[test.cpp:4]: (error) Pointer to local array variable returned.\n", errout.str());
check("char *foo()\n" // use ValueFlow
"{\n"
" char str[100] = {0};\n"
" char *p = str;\n"
" return p;\n"
"}");
ASSERT_EQUALS("[test.cpp:5]: (error) Pointer to local array variable returned.\n", errout.str());
check("class Fred {\n"
" char *foo();\n"
"};\n"