Fix FP due to namespace scope (#3475)

This commit is contained in:
KenPatrickLehrmann 2021-10-04 23:16:16 +02:00 committed by GitHub
parent 428144c53c
commit 61cddabe74
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 71 additions and 1 deletions

View File

@ -3194,7 +3194,7 @@ static const Token* getEndOfVarScope(const Token* tok, const std::vector<const V
{
const Token* endOfVarScope = nullptr;
for (const Variable* var : vars) {
if (var && (var->isLocal() || 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;

View File

@ -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() {