value flow: Fixed FP for division then check in for loop: 'for (a=b/x;x>0;x--)'

This commit is contained in:
Daniel Marjamäki 2014-01-25 09:22:32 +01:00
parent 4647a9fc93
commit 35b51468cb
2 changed files with 16 additions and 2 deletions

View File

@ -273,7 +273,7 @@ static void valueFlowBeforeCondition(TokenList *tokenlist, ErrorLogger *errorLog
continue;
}
// bailout: while-condition, variable is changed in while loop
// bailout: for/while-condition, variable is changed in while loop
for (const Token *tok2 = tok; tok2; tok2 = tok2->astParent()) {
if (tok2->astParent() || tok2->str() != "(" || !Token::simpleMatch(tok2->link(), ") {"))
continue;
@ -281,7 +281,14 @@ static void valueFlowBeforeCondition(TokenList *tokenlist, ErrorLogger *errorLog
if (Token::Match(tok2->previous(), "for|while (")) {
const Token *start = tok2->link()->next();
const Token *end = start->link();
if (Token::findmatch(start,"++|--| %varid% ++|--|=",end,varid)) {
if (tok2->astOperand2()->str() == ";" &&
tok2->astOperand2()->astOperand2() &&
tok2->astOperand2()->astOperand2()->str() == ";")
start = tok2->astOperand2()->astOperand2();
if (Token::findmatch(start,"++|-- %varid%",end,varid) ||
Token::findmatch(start,"%varid% ++|--|=",end,varid)) {
varid = 0U;
if (settings->debugwarnings)
bailout(tokenlist, errorLogger, tok, "variable " + var->nameToken()->str() + " used in loop");

View File

@ -333,6 +333,13 @@ private:
" }\n"
"}";
ASSERT_EQUALS(false, testValueOfX(code, 2U, 0));
code = "void f(unsigned int x) {\n"
" for (int a = 1000 / x;\n"
" x > 0;\n"
" x--) {}\n"
"}";
ASSERT_EQUALS(false, testValueOfX(code, 2U, 0));
}
void valueFlowBeforeConditionTernaryOp() { // bailout: ?: