From 61cddabe745b97599933027d60730111b765e1d2 Mon Sep 17 00:00:00 2001 From: KenPatrickLehrmann <54900810+KenPatrickLehrmann@users.noreply.github.com> Date: Mon, 4 Oct 2021 23:16:16 +0200 Subject: [PATCH] Fix FP due to namespace scope (#3475) --- lib/valueflow.cpp | 2 +- test/testautovariables.cpp | 70 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 1 deletion(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index d4b0065be..0167c0d1c 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -3194,7 +3194,7 @@ static const Token* getEndOfVarScope(const Token* tok, const std::vectorisLocal() || var->isArgument())) + if (var && (var->isLocal() || var->isArgument()) && var->typeStartToken()->scope()->type != Scope::eNamespace) endOfVarScope = var->typeStartToken()->scope()->bodyEnd; else if (!endOfVarScope) endOfVarScope = tok->scope()->bodyEnd; diff --git a/test/testautovariables.cpp b/test/testautovariables.cpp index 8dd9439f9..cad3a05ed 100644 --- a/test/testautovariables.cpp +++ b/test/testautovariables.cpp @@ -3117,6 +3117,76 @@ private: " }\n" "};"); ASSERT_EQUALS("", errout.str()); + + check("namespace test {\n" + "class Foo {};\n" + "struct Bar {\n" + " Foo *_foo;\n" + "};\n" + "\n" + "int f(Bar *bar);\n" + "\n" + "void g(Bar *bar) {\n" + " {\n" + " Foo foo;\n" + " bar->_foo = &foo;\n" + " bar->_foo = nullptr;\n" + " }\n" + " f(bar);\n" + "}\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + + check("class Foo {};\n" + "struct Bar {\n" + " Foo *_foo;\n" + "};\n" + "\n" + "int f(Bar *bar);\n" + "\n" + "void g(Bar *bar) {\n" + " {\n" + " Foo foo;\n" + " bar->_foo = &foo;\n" + " bar->_foo = nullptr;\n" + " }\n" + " f(bar);\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + + check("namespace test {\n" + "class Foo {};\n" + "struct Bar {\n" + " Foo *_foo;\n" + "};\n" + "\n" + "int f(Bar *bar);\n" + "\n" + "void g(Bar *bar) {\n" + " {\n" + " Foo foo;\n" + " bar->_foo = &foo;\n" + " }\n" + " f(bar);\n" + "}\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:12]: (error) Address of local auto-variable assigned to a function parameter.\n", errout.str()); + + check("class Foo {};\n" + "struct Bar {\n" + " Foo *_foo;\n" + "};\n" + "\n" + "int f(Bar *bar);\n" + "\n" + "void g(Bar *bar) {\n" + " {\n" + " Foo foo;\n" + " bar->_foo = &foo;\n" + " }\n" + " f(bar);\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:11]: (error) Address of local auto-variable assigned to a function parameter.\n", errout.str()); } void deadPointer() {