Fixed #8182 (False positive uninitvar - variable initialized in function in ternary expression)

This commit is contained in:
Daniel Marjamäki 2018-03-16 19:13:48 +01:00
parent 449dcc15e8
commit ec6133aea2
3 changed files with 23 additions and 1 deletions

View File

@ -1899,6 +1899,7 @@ static bool valueFlowForward(Token * const startToken,
}
// Skip conditional expressions..
const Token * const questionToken = tok2;
while (tok2->astOperand1() || tok2->astOperand2()) {
if (tok2->astOperand2())
tok2 = const_cast<Token*>(tok2->astOperand2());
@ -1908,6 +1909,14 @@ static bool valueFlowForward(Token * const startToken,
break;
}
tok2 = tok2->next();
if (isVariableChanged(questionToken, questionToken->astOperand2(), varid, false, settings) &&
isVariableChanged(questionToken->astOperand2(), tok2, varid, false, settings)) {
if (settings->debugwarnings)
bailout(tokenlist, errorLogger, tok2, "variable " + var->name() + " valueFlowForward, assignment in condition");
return false;
}
}
else if (tok2->varId() == varid) {

View File

@ -203,7 +203,7 @@ private:
check("int a=sizeof(foo())");
ASSERT_EQUALS("", errout.str());
check("int foo(int) { return 1; }; char buf[1024]; int a=sizeof(buf), foo(0)");
ASSERT_EQUALS("", errout.str());

View File

@ -80,6 +80,7 @@ private:
TEST_CASE(valueFlowForwardCompoundAssign);
TEST_CASE(valueFlowForwardCorrelatedVariables);
TEST_CASE(valueFlowForwardFunction);
TEST_CASE(valueFlowForwardTernary);
TEST_CASE(valueFlowForwardLambda);
TEST_CASE(valueFlowSwitchVariable);
@ -1837,6 +1838,18 @@ private:
ASSERT_EQUALS(true, testValueOfX(code, 8U, 1));
}
void valueFlowForwardTernary() {
const char *code;
code = "int f() {\n"
" int x=5;\n"
" a = b ? init1(&x) : init2(&x);\n"
" return 1 + x;\n"
"}";
ASSERT_EQUALS(true, testValueOfX(code, 3U, 5));
ASSERT_EQUALS(false, testValueOfX(code, 4U, 5));
}
void valueFlowForwardLambda() {
const char *code;