Fixed #5434 (FP: Out-of-bounds access with ternary operator in loop)

This commit is contained in:
Daniel Marjamäki 2014-02-22 17:58:48 +01:00
parent 34730f623a
commit f6b42633e8
2 changed files with 22 additions and 0 deletions

View File

@ -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);

View File

@ -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() {