From 95a48eac67c30852e0015669d33d8989dde83052 Mon Sep 17 00:00:00 2001 From: Paul Fultz II Date: Sun, 16 Feb 2020 08:56:52 -0600 Subject: [PATCH] Fix issue 9598: False positive: Using iterator to local container that may be invalid for loop handling (#2539) --- lib/checkstl.cpp | 14 ++++++++++++-- test/teststl.cpp | 7 +++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index 4db081998..db30455a1 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -749,8 +749,18 @@ void CheckStl::invalidContainer() continue; std::set skipVarIds; // Skip if the variable is assigned to - if (Token::Match(tok->astTop(), "%assign%") && Token::Match(tok->astTop()->previous(), "%var%")) - skipVarIds.insert(tok->astTop()->previous()->varId()); + const Token* assignExpr = tok; + while (assignExpr->astParent()) { + bool isRHS = astIsRHS(assignExpr); + assignExpr = assignExpr->astParent(); + if (Token::Match(assignExpr, "%assign%")) { + if (!isRHS) + assignExpr = nullptr; + break; + } + } + if (Token::Match(assignExpr, "%assign%") && Token::Match(assignExpr->astOperand1(), "%var%")) + skipVarIds.insert(assignExpr->astOperand1()->varId()); const Token * endToken = nextAfterAstRightmostLeaf(tok->next()->astParent()); if (!endToken) endToken = tok->next(); diff --git a/test/teststl.cpp b/test/teststl.cpp index f6c83cd3b..3ca8cfab1 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -4111,6 +4111,13 @@ private: " }\n" "}\n",true); ASSERT_EQUALS("", errout.str()); + + // #9598 + check("void f(std::vector v) {\n" + " for (auto it = v.begin(); it != v.end(); it = v.erase(it))\n" + " *it;\n" + "}\n",true); + ASSERT_EQUALS("", errout.str()); } void findInsert() {