From 146bf11aa705e76c9aeedda330374fae91e5801a Mon Sep 17 00:00:00 2001 From: Alexander Mai Date: Mon, 12 May 2014 19:53:49 +0200 Subject: [PATCH] #5793 - False positive: Deallocation of an auto-variable (at reference notation) --- lib/checkautovariables.cpp | 2 +- test/testautovariables.cpp | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) 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() {