Fix issue 9780: FP: invalidContainer calling push_back after getting the address of the vector

This commit is contained in:
Paul 2020-08-06 21:08:30 -05:00
parent 72fa5f2e27
commit 56affc9080
4 changed files with 18 additions and 2 deletions

View File

@ -854,6 +854,8 @@ void CheckStl::invalidContainer()
return false;
if (skipVarIds.count(info.tok->varId()) > 0)
return false;
// if (Token::simpleMatch(info.tok->next(), "."))
// return false;
if (Token::Match(info.tok->astParent(), "%assign%") && astIsLHS(info.tok))
skipVarIds.insert(info.tok->varId());
if (info.tok->variable()->isReference() &&
@ -878,6 +880,8 @@ void CheckStl::invalidContainer()
continue;
if (val.lifetimeKind == ValueFlow::Value::LifetimeKind::Address)
continue;
if (val.lifetimeKind == ValueFlow::Value::LifetimeKind::SubObject)
continue;
if (!val.tokvalue->variable())
continue;
if (val.tokvalue->varId() != tok->varId())

View File

@ -2836,6 +2836,7 @@ std::string lifetimeType(const Token *tok, const ValueFlow::Value *val)
result = "iterator";
break;
case ValueFlow::Value::LifetimeKind::Object:
case ValueFlow::Value::LifetimeKind::SubObject:
case ValueFlow::Value::LifetimeKind::Address:
if (astIsPointer(tok))
result = "pointer";
@ -2858,6 +2859,7 @@ std::string lifetimeMessage(const Token *tok, const ValueFlow::Value *val, Error
const Variable * var = vartok->variable();
if (var) {
switch (val->lifetimeKind) {
case ValueFlow::Value::LifetimeKind::SubObject:
case ValueFlow::Value::LifetimeKind::Object:
case ValueFlow::Value::LifetimeKind::Address:
if (type == "pointer")
@ -3209,7 +3211,7 @@ static void valueFlowForwardLifetime(Token * tok, TokenList *tokenlist, ErrorLog
for (ValueFlow::Value& val : values) {
if (val.lifetimeKind == ValueFlow::Value::LifetimeKind::Address)
val.lifetimeKind = ValueFlow::Value::LifetimeKind::Object;
val.lifetimeKind = ValueFlow::Value::LifetimeKind::SubObject;
}
}
for (const Variable* var : vars) {

View File

@ -252,7 +252,7 @@ namespace ValueFlow {
/** Path id */
MathLib::bigint path;
enum class LifetimeKind {Object, Lambda, Iterator, Address} lifetimeKind;
enum class LifetimeKind {Object, SubObject, Lambda, Iterator, Address} lifetimeKind;
enum class LifetimeScope { Local, Argument } lifetimeScope;

View File

@ -4198,6 +4198,16 @@ private:
" delete b;\n"
"}\n",true);
ASSERT_EQUALS("", errout.str());
// #9780
check("int f() {\n"
" std::vector<int> vect;\n"
" MyStruct info{};\n"
" info.vect = &vect;\n"
" vect.push_back(1);\n"
" return info.ret;\n"
"}\n",true);
ASSERT_EQUALS("", errout.str());
}
void invalidContainerLoop() {