Fix 10264: FP invalidContainer when address of container is passed inside struct (#3286)

This commit is contained in:
Paul Fultz II 2021-06-04 10:20:47 -05:00 committed by GitHub
parent b23c5aa742
commit 537fb5bcd9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 2 deletions

View File

@ -3499,7 +3499,7 @@ static void valueFlowLifetimeConstructor(Token* tok,
// constructor, but make each lifetime inconclusive
std::vector<const Token*> args = getArguments(tok);
LifetimeStore::forEach(
args, "Passed to initializer list.", ValueFlow::Value::LifetimeKind::Object, [&](LifetimeStore& ls) {
args, "Passed to initializer list.", ValueFlow::Value::LifetimeKind::SubObject, [&](LifetimeStore& ls) {
ls.inconclusive = true;
ls.byVal(tok, tokenlist, errorLogger, settings);
});
@ -3514,7 +3514,7 @@ static void valueFlowLifetimeConstructor(Token* tok,
auto it = scope->varlist.begin();
LifetimeStore::forEach(args,
"Passed to constructor of '" + t->name() + "'.",
ValueFlow::Value::LifetimeKind::Object,
ValueFlow::Value::LifetimeKind::SubObject,
[&](const LifetimeStore& ls) {
if (it == scope->varlist.end())
return;

View File

@ -4665,6 +4665,17 @@ private:
ASSERT_EQUALS(
"[test.cpp:1] -> [test.cpp:2] -> [test.cpp:3] -> [test.cpp:7] -> [test.cpp:1] -> [test.cpp:4]: (error) Using iterator to local container 'v' that may be invalid.\n",
errout.str());
// #10264
check("void f(std::vector<std::string>& x) {\n"
" struct I {\n"
" std::vector<std::string> *px{};\n"
" };\n"
" I i = { &x };\n"
" x.clear();\n"
" Parse(i);\n"
"}\n",true);
ASSERT_EQUALS("", errout.str());
}
void invalidContainerLoop() {