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

This commit is contained in:
Paul Fultz II 2021-07-30 08:52:00 -05:00 committed by GitHub
parent 9d51b4450b
commit 6a81b4c17c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 2 deletions

View File

@ -3515,13 +3515,13 @@ static void valueFlowLifetimeConstructor(Token* tok, TokenList* tokenlist, Error
// Assume range constructor if passed a pair of iterators // Assume range constructor if passed a pair of iterators
if (astIsContainer(parent) && args.size() == 2 && astIsIterator(args[0]) && astIsIterator(args[1])) { if (astIsContainer(parent) && args.size() == 2 && astIsIterator(args[0]) && astIsIterator(args[1])) {
LifetimeStore::forEach( LifetimeStore::forEach(
args, "Passed to initializer list.", ValueFlow::Value::LifetimeKind::Object, [&](const LifetimeStore& ls) { args, "Passed to initializer list.", ValueFlow::Value::LifetimeKind::SubObject, [&](const LifetimeStore& ls) {
ls.byDerefCopy(tok, tokenlist, errorLogger, settings); ls.byDerefCopy(tok, tokenlist, errorLogger, settings);
}); });
} else { } else {
LifetimeStore::forEach(args, LifetimeStore::forEach(args,
"Passed to initializer list.", "Passed to initializer list.",
ValueFlow::Value::LifetimeKind::Object, ValueFlow::Value::LifetimeKind::SubObject,
[&](const LifetimeStore& ls) { [&](const LifetimeStore& ls) {
ls.byVal(tok, tokenlist, errorLogger, settings); ls.byVal(tok, tokenlist, errorLogger, settings);
}); });

View File

@ -4768,6 +4768,22 @@ private:
" Parse(i);\n" " Parse(i);\n"
"}\n",true); "}\n",true);
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
check("void f() {\n"
" std::string x;\n"
" struct V {\n"
" std::string* pStr{};\n"
" };\n"
" struct I {\n"
" std::vector<V> v;\n"
" };\n"
" I b[] = {{{{ &x }}}};\n"
" x = \"Arial\";\n"
" I cb[1];\n"
" for (long i = 0; i < 1; ++i)\n"
" cb[i] = b[i];\n"
"}\n",true);
ASSERT_EQUALS("", errout.str());
} }
void invalidContainerLoop() { void invalidContainerLoop() {