From 35b51468cb1306ff0a9072f25fecf0ff556705eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 25 Jan 2014 09:22:32 +0100 Subject: [PATCH] value flow: Fixed FP for division then check in for loop: 'for (a=b/x;x>0;x--)' --- lib/valueflow.cpp | 11 +++++++++-- test/testvalueflow.cpp | 7 +++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index c6f11f1ba..6625c33d8 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -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"); diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index 03e268373..fd3ca43ff 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -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: ?: