From 6a81b4c17c87241c6f205e9d47ebc9b5af7f0f38 Mon Sep 17 00:00:00 2001 From: Paul Fultz II Date: Fri, 30 Jul 2021 08:52:00 -0500 Subject: [PATCH] Fix 10264: FP invalidContainer when address of container is passed inside struct (#3368) --- lib/valueflow.cpp | 4 ++-- test/teststl.cpp | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 69cebc518..24c059081 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -3515,13 +3515,13 @@ static void valueFlowLifetimeConstructor(Token* tok, TokenList* tokenlist, Error // Assume range constructor if passed a pair of iterators if (astIsContainer(parent) && args.size() == 2 && astIsIterator(args[0]) && astIsIterator(args[1])) { 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); }); } else { LifetimeStore::forEach(args, "Passed to initializer list.", - ValueFlow::Value::LifetimeKind::Object, + ValueFlow::Value::LifetimeKind::SubObject, [&](const LifetimeStore& ls) { ls.byVal(tok, tokenlist, errorLogger, settings); }); diff --git a/test/teststl.cpp b/test/teststl.cpp index ac0d38de4..97b7b8194 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -4768,6 +4768,22 @@ private: " Parse(i);\n" "}\n",true); 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;\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() {