diff --git a/lib/checkautovariables.cpp b/lib/checkautovariables.cpp index 5c46444e7..287634bd3 100644 --- a/lib/checkautovariables.cpp +++ b/lib/checkautovariables.cpp @@ -202,7 +202,7 @@ void CheckAutoVariables::autoVariables() errorInvalidDeallocation(tok); } else if (Token::Match(tok, "free ( & %var% ) ;") || (_tokenizer->isCPP() && Token::Match(tok, "delete [| ]| (| & %var% !!["))) { tok = Token::findmatch(tok->next(), "%var%"); - if (tok && tok->variable() && tok->variable()->isLocal()) + if (isAutoVar(tok)) errorInvalidDeallocation(tok); } } diff --git a/test/testautovariables.cpp b/test/testautovariables.cpp index 9cd892e44..0bed5061a 100644 --- a/test/testautovariables.cpp +++ b/test/testautovariables.cpp @@ -515,6 +515,29 @@ private: "}"); TODO_ASSERT_EQUALS("[test.cpp:3]: (error) Deallocation of an auto-variable results in undefined behaviour.\n", "", errout.str()); + check("int main() {\n" + " long *pKoeff[256];\n" + " free (pKoeff);\n" + "}"); + TODO_ASSERT_EQUALS("[test.cpp:3]: (error) Deallocation of an auto-variable results in undefined behaviour.\n", "", errout.str()); + + check("void foo() {\n" + " int& intref = Getter();\n" + " delete *intref;\n" + "}"); + ASSERT_EQUALS("", errout.str()); + check("void foo() {\n" + " FOO& fooref = Getter();\n" + " delete *fooref;\n" + "}"); + ASSERT_EQUALS("", errout.str()); + + check("void test() {\n" + " MyObj& obj = *new MyObj;\n" + " delete &obj;\n" + "}"); + ASSERT_EQUALS("", errout.str()); + } void testinvaliddealloc_C() {