diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 1acaf0f9b..bdbd5d9c5 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -665,6 +665,19 @@ static void valueFlowForLoop(TokenList *tokenlist, ErrorLogger *errorLogger, con for (Token *tok2 = bodyStart->next(); tok2 != bodyEnd; tok2 = tok2->next()) { if (tok2->varId() == vartok->varId()) { + const Token * parent = tok2->astParent(); + while (parent) { + const Token * const p = parent; + parent = parent->astParent(); + if (parent && parent->str() == "?" && parent->astOperand2() == p) + break; + } + if (parent) { + if (settings->debugwarnings) + bailout(tokenlist, errorLogger, tok2, "For loop variable " + vartok->str() + " stopping on ?"); + continue; + } + ValueFlow::Value value1(num1); value1.varId = tok2->varId(); setTokenValue(tok2, value1); diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index ba1122e69..db65c38e8 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -624,6 +624,15 @@ private: ASSERT_EQUALS(true, testValueOfX(code, 3U, 0)); ASSERT_EQUALS(true, testValueOfX(code, 3U, 9)); ASSERT_EQUALS(false, testValueOfX(code, 3U, 10)); + + code = "void f() {\n" + " for (int x = 0; x < 10; x++)\n" + " x<4 ?\n" + " a[x] : 0;\n" + "}"; + ASSERT_EQUALS(true, testValueOfX(code, 3U, 0)); + ASSERT_EQUALS(true, testValueOfX(code, 3U, 9)); + ASSERT_EQUALS(false, testValueOfX(code, 4U, 9)); } void valueFlowSubFunction() {