Merge pull request #2732 from pfultz2/invalid-container-subobj

Fix issue 9780: FP: invalidContainer calling push_back after getting the address of the vector
This commit is contained in:
Daniel Marjamäki 2020-08-07 09:52:25 +02:00 committed by GitHub
commit b263b93f73
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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() {