Fixed #6330 (valueflow: condition is always false first iteration in dowhile loop)

This commit is contained in:
Daniel Marjamäki 2016-01-24 08:57:57 +01:00
parent abccd9af95
commit 76cdfbf487
2 changed files with 17 additions and 14 deletions

View File

@ -1089,6 +1089,13 @@ static bool valueFlowForward(Token * const startToken,
const Token *end = start->link(); const Token *end = start->link();
if (Token::simpleMatch(end, "} while (")) if (Token::simpleMatch(end, "} while ("))
end = end->linkAt(2); end = end->linkAt(2);
if (isVariableChanged(start, end, varid)) {
if (settings->debugwarnings)
bailout(tokenlist, errorLogger, tok2, "variable " + var->name() + " valueFlowForward, assignment in do-while");
return false;
}
handleKnownValuesInLoop(start, end, &values, varid); handleKnownValuesInLoop(start, end, &values, varid);
} }

View File

@ -1065,6 +1065,15 @@ private:
"}"; "}";
ASSERT_EQUALS(false, testValueOfX(code, 3U, 87)); ASSERT_EQUALS(false, testValueOfX(code, 3U, 87));
code = "void f() {\n"
" int first=-1, x=0;\n"
" do {\n"
" if (first >= 0) { a = x; }\n" // <- x is not 0
" first++; x=3;\n"
" } while (1);\n"
"}";
ASSERT_EQUALS(false, testValueOfX(code, 4U, 0));
// pointer/reference to x // pointer/reference to x
code = "int f(void) {\n" code = "int f(void) {\n"
" int x = 2;\n" " int x = 2;\n"
@ -1709,14 +1718,11 @@ private:
bool isNotKnownValues(const char code[], const char str[]) { bool isNotKnownValues(const char code[], const char str[]) {
const std::list<ValueFlow::Value> values = tokenValues(code, str); const std::list<ValueFlow::Value> values = tokenValues(code, str);
bool possible = false;
for (std::list<ValueFlow::Value>::const_iterator it = values.begin(); it != values.end(); ++it) { for (std::list<ValueFlow::Value>::const_iterator it = values.begin(); it != values.end(); ++it) {
if (it->isKnown()) if (it->isKnown())
return false; return false;
if (it->isPossible())
possible = true;
} }
return possible; return true;
} }
void knownValue() { void knownValue() {
@ -1831,16 +1837,6 @@ private:
ASSERT_EQUALS(1, value.intvalue); ASSERT_EQUALS(1, value.intvalue);
ASSERT(value.isPossible()); ASSERT(value.isPossible());
code = "void f() {\n"
" int x = 0;\n"
" do {\n"
" if (!x) { x = y; }\n" // <- possible value
" } while (count < 10);\n"
"}";
value = valueOfTok(code, "!");
ASSERT_EQUALS(1, value.intvalue);
ASSERT(value.isPossible());
code = "void f() {\n" code = "void f() {\n"
" int x = 0;\n" " int x = 0;\n"
"a:\n" "a:\n"