Skip to content

Commit c10a10c

Browse files
committed
CheckAutoVariables: use ValueFlow to detect more errors when pointer aliases are used
1 parent 573edb4 commit c10a10c

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

lib/checkautovariables.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,21 @@ bool CheckAutoVariables::isAutoVar(const Token *tok)
8888

8989
bool CheckAutoVariables::isAutoVarArray(const Token *tok)
9090
{
91+
// Variable
9192
const Variable *var = tok->variable();
93+
if (var && var->isLocal() && !var->isStatic() && var->isArray() && !var->isPointer())
94+
return true;
9295

93-
return (var && var->isLocal() && !var->isStatic() && var->isArray() && !var->isPointer());
96+
// ValueFlow
97+
if (var && var->isPointer()) {
98+
for (std::list<ValueFlow::Value>::const_iterator it = tok->values.begin(); it != tok->values.end(); ++it) {
99+
const ValueFlow::Value &val = *it;
100+
if (val.tokvalue && isAutoVarArray(val.tokvalue))
101+
return true;
102+
}
103+
}
104+
105+
return false;
94106
}
95107

96108
// Verification that we really take the address of a local variable

test/testautovariables.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,14 @@ class TestAutoVariables : public TestFixture {
634634
"}");
635635
ASSERT_EQUALS("[test.cpp:4]: (error) Pointer to local array variable returned.\n", errout.str());
636636

637+
check("char *foo()\n" // use ValueFlow
638+
"{\n"
639+
" char str[100] = {0};\n"
640+
" char *p = str;\n"
641+
" return p;\n"
642+
"}");
643+
ASSERT_EQUALS("[test.cpp:5]: (error) Pointer to local array variable returned.\n", errout.str());
644+
637645
check("class Fred {\n"
638646
" char *foo();\n"
639647
"};\n"

0 commit comments

Comments
 (0)