From 537fb5bcd9c4593d0eee3ad40f79bf8c9208bd20 Mon Sep 17 00:00:00 2001 From: Paul Fultz II Date: Fri, 4 Jun 2021 10:20:47 -0500 Subject: [PATCH] Fix 10264: FP invalidContainer when address of container is passed inside struct (#3286) --- lib/valueflow.cpp | 4 ++-- test/teststl.cpp | 11 +++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index cd16592c3..6e873333d 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -3499,7 +3499,7 @@ static void valueFlowLifetimeConstructor(Token* tok, // constructor, but make each lifetime inconclusive std::vector 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; diff --git a/test/teststl.cpp b/test/teststl.cpp index ac10d4f08..e9579bc72 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -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& x) {\n" + " struct I {\n" + " std::vector *px{};\n" + " };\n" + " I i = { &x };\n" + " x.clear();\n" + " Parse(i);\n" + "}\n",true); + ASSERT_EQUALS("", errout.str()); } void invalidContainerLoop() {