Fix 9777: False Positive: Condition is always true with reset/release on unique_ptr (#3440)

This commit is contained in:
Paul Fultz II 2021-09-04 12:06:13 -05:00 committed by GitHub
parent 8a708e556c
commit 9eb5eadd35
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 0 deletions

View File

@ -6790,6 +6790,17 @@ static void valueFlowSmartPointer(TokenList *tokenlist, ErrorLogger * errorLogge
valueFlowForwardAssign(inTok, var, values, constValue, false, tokenlist, errorLogger, settings);
}
} else if (Token::Match(tok, "%var% . release ( )") && tok->next()->originalName() != "->") {
const Token* parent = tok->tokAt(3)->astParent();
bool hasParentReset = false;
while (parent) {
if (Token::Match(parent->tokAt(-3), "%varid% . release|reset (", tok->varId())) {
hasParentReset = true;
break;
}
parent = parent->astParent();
}
if (hasParentReset)
continue;
std::list<ValueFlow::Value> values;
ValueFlow::Value v(0);
v.setKnown();

View File

@ -143,6 +143,7 @@ private:
TEST_CASE(valueFlowUnsigned);
TEST_CASE(valueFlowMod);
TEST_CASE(valueFlowSymbolic);
TEST_CASE(valueFlowSmartPointer);
}
static bool isNotTokValue(const ValueFlow::Value &val) {
@ -6308,6 +6309,19 @@ private:
ASSERT_EQUALS(true, testValueOfX(code, 3U, "y", 0));
ASSERT_EQUALS(true, testValueOfXImpossible(code, 3U, "y", -1));
}
void valueFlowSmartPointer()
{
const char* code;
code = "int* df(int* expr);\n"
"int * f() {\n"
" std::unique_ptr<int> x;\n"
" x.reset(df(x.release()));\n"
" return x;\n"
"}\n";
ASSERT_EQUALS(false, testValueOfX(code, 5U, 0));
}
};
REGISTER_TEST(TestValueFlow)