Find dangling pointers to unique_ptr (#3486)

This commit is contained in:
Paul Fultz II 2021-10-06 01:46:25 -05:00 committed by GitHub
parent 3cb252bd99
commit 25eb0ab5bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 0 deletions

View File

@ -4027,6 +4027,19 @@ static void valueFlowLifetime(TokenList *tokenlist, SymbolDatabase*, ErrorLogger
else if (Token::Match(tok, "%name% (")) {
valueFlowLifetimeFunction(tok, tokenlist, errorLogger, settings);
}
// Unique pointer lifetimes
else if (isUniqueSmartPointer(tok) && astIsLHS(tok) && Token::simpleMatch(tok->astParent(), ". get ( )")) {
Token* ptok = tok->astParent()->tokAt(2);
ErrorPath errorPath = {{ptok, "Raw pointer to smart pointer created here."}};
ValueFlow::Value value;
value.valueType = ValueFlow::Value::ValueType::LIFETIME;
value.lifetimeScope = ValueFlow::Value::LifetimeScope::Local;
value.lifetimeKind = ValueFlow::Value::LifetimeKind::SubObject;
value.tokvalue = tok;
value.errorPath = errorPath;
setTokenValue(ptok, value, tokenlist->getSettings());
valueFlowForwardLifetime(ptok, tokenlist, errorLogger, settings);
}
// Check variables
else if (tok->variable()) {
ErrorPath errorPath;

View File

@ -142,6 +142,7 @@ private:
TEST_CASE(danglingLifetimeLambda);
TEST_CASE(danglingLifetimeContainer);
TEST_CASE(danglingLifetimeContainerView);
TEST_CASE(danglingLifetimeUniquePtr);
TEST_CASE(danglingLifetime);
TEST_CASE(danglingLifetimeFunction);
TEST_CASE(danglingLifetimeAggegrateConstructor);
@ -2541,6 +2542,16 @@ private:
ASSERT_EQUALS("", errout.str());
}
void danglingLifetimeUniquePtr()
{
check("int* f(std::unique_ptr<int> p) {\n"
" int * rp = p.get();\n"
" return rp;\n"
"}\n");
ASSERT_EQUALS(
"[test.cpp:2] -> [test.cpp:1] -> [test.cpp:3]: (error) Returning pointer to local variable 'p' that will be invalid when returning.\n",
errout.str());
}
void danglingLifetime() {
check("auto f() {\n"
" std::vector<int> a;\n"