Fix issue 9598: False positive: Using iterator to local container that may be invalid for loop handling (#2539)

This commit is contained in:
Paul Fultz II 2020-02-16 08:56:52 -06:00 committed by GitHub
parent a350ed9bc2
commit 95a48eac67
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 2 deletions

View File

@ -749,8 +749,18 @@ void CheckStl::invalidContainer()
continue;
std::set<nonneg int> 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();

View File

@ -4111,6 +4111,13 @@ private:
" }\n"
"}\n",true);
ASSERT_EQUALS("", errout.str());
// #9598
check("void f(std::vector<std::string> 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() {