Fix 9777: False Positive: Condition is always true with reset/release on unique_ptr (#3440)
This commit is contained in:
parent
8a708e556c
commit
9eb5eadd35
|
@ -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();
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue