Fix 10456: FP identicalConditionAfterEarlyExit with variable captured by reference (#3439)

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

View File

@ -654,6 +654,8 @@ void CheckCondition::multiCondition2()
const Token * const endToken = tok->scope()->bodyEnd;
for (; tok && tok != endToken; tok = tok->next()) {
if (isExpressionChangedAt(cond1, tok, 0, false, mSettings, mTokenizer->isCPP()))
break;
if (Token::Match(tok, "if|return")) {
const Token * condStartToken = tok->str() == "if" ? tok->next() : tok;
const Token * condEndToken = tok->str() == "if" ? condStartToken->link() : Token::findsimplematch(condStartToken, ";");

View File

@ -2131,6 +2131,10 @@ struct ValueFlowAnalyzer : Analyzer {
}
virtual Action isAliasModified(const Token* tok) const {
// Lambda function call
if (Token::Match(tok, "%var% ("))
// TODO: Check if modified in the lambda function
return Action::Invalid;
int indirect = 0;
if (tok->valueType())
indirect = tok->valueType()->pointer;

View File

@ -2676,6 +2676,17 @@ private:
" return ret;\n"
"}");
ASSERT_EQUALS("", errout.str());
// #10456
check("int f() {\n"
" int i = 0;\n"
" auto f = [&](bool b) { if (b) ++i; };\n"
" if (i) return i;\n"
" f(true);\n"
" if (i) return i;\n"
" return 0;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void innerConditionModified() {

View File

@ -3242,6 +3242,16 @@ private:
" f();\n"
"}";
TODO_ASSERT_EQUALS(true, false, testValueOfX(code, 3U, 3));
code = "void f() {\n"
" int x=3;\n"
" auto f = [&](){ x++; }\n"
" x = 1;\n"
" f();\n"
" int a = x;\n" // x is actually 2
"}";
ASSERT_EQUALS(false, testValueOfX(code, 6U, 1));
ASSERT_EQUALS(false, testValueOfX(code, 6U, 3));
}
void valueFlowForwardTryCatch() {