File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -88,9 +88,21 @@ bool CheckAutoVariables::isAutoVar(const Token *tok)
8888
8989bool 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
Original file line number Diff line number Diff 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 "
You can’t perform that action at this time.
0 commit comments