Fix 10641: FP invalidLifetime with compiler-generated constructor (#3609)

This commit is contained in:
Paul Fultz II 2021-12-07 00:43:25 -06:00 committed by GitHub
parent 0be6e27231
commit 3874c546cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 2 deletions

View File

@ -3631,8 +3631,6 @@ struct LifetimeStore {
const Variable* var = lt.token->variable(); const Variable* var = lt.token->variable();
if (var && var->isArgument()) { if (var && var->isArgument()) {
value.lifetimeScope = ValueFlow::Value::LifetimeScope::Argument; value.lifetimeScope = ValueFlow::Value::LifetimeScope::Argument;
} else if (exprDependsOnThis(lt.token)) {
value.lifetimeScope = ValueFlow::Value::LifetimeScope::ThisPointer;
} else { } else {
continue; continue;
} }

View File

@ -3398,6 +3398,23 @@ private:
ASSERT_EQUALS( ASSERT_EQUALS(
"[test.cpp:9] -> [test.cpp:9] -> [test.cpp:9] -> [test.cpp:4] -> [test.cpp:4] -> [test.cpp:8] -> [test.cpp:9]: (error) Returning object that points to local variable 'i' that will be invalid when returning.\n", "[test.cpp:9] -> [test.cpp:9] -> [test.cpp:9] -> [test.cpp:4] -> [test.cpp:4] -> [test.cpp:8] -> [test.cpp:9]: (error) Returning object that points to local variable 'i' that will be invalid when returning.\n",
errout.str()); errout.str());
check("struct S {\n"
" int i{};\n"
"};\n"
"struct T {\n"
" S getS() const { return S{ j }; }\n"
" int j{};\n"
"};\n"
"void f(S* p) {\n"
" S ret;\n"
" {\n"
" T t;\n"
" ret = t.getS();\n"
" }\n"
" *p = ret;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
} }
void invalidLifetime() { void invalidLifetime() {