Fixed #3359 (False positive: array[idx++] += val triggers unexpected 'Array index out of bounds' error)

This commit is contained in:
Daniel Marjamäki 2011-12-04 12:22:21 +01:00
parent 49784a44e1
commit eeb6dc48a5
2 changed files with 18 additions and 1 deletions

View File

@ -5038,7 +5038,22 @@ void Tokenizer::simplifyCompoundAssignment()
tok->insertToken(op); tok->insertToken(op);
std::stack<Token *> tokend; std::stack<Token *> tokend;
for (const Token *tok2 = tok->previous(); tok2 && tok2 != tok1; tok2 = tok2->previous()) { for (Token *tok2 = tok->previous(); tok2 && tok2 != tok1; tok2 = tok2->previous()) {
// Don't duplicate ++ and --. Put preincrement in lhs. Put
// postincrement in rhs.
if (tok2->str() == "++" || tok2->str() == "--") {
// pre increment/decrement => don't copy
if (tok2->next()->isName()) {
continue;
}
// post increment/decrement => move from lhs to rhs
tok->insertToken(tok2->str());
tok2->deleteThis();
continue;
}
// Copy token from lhs to rhs
tok->insertToken(tok2->str()); tok->insertToken(tok2->str());
tok->next()->varId(tok2->varId()); tok->next()->varId(tok2->varId());
if (Token::Match(tok->next(), "]|)")) if (Token::Match(tok->next(), "]|)"))

View File

@ -5796,6 +5796,8 @@ private:
ASSERT_EQUALS("; x [ 0 ] = x [ 0 ] + 1 ;", tokenizeAndStringify("; x[0] += 1;")); ASSERT_EQUALS("; x [ 0 ] = x [ 0 ] + 1 ;", tokenizeAndStringify("; x[0] += 1;"));
ASSERT_EQUALS("; x [ y - 1 ] = x [ y - 1 ] + 1 ;", tokenizeAndStringify("; x[y-1] += 1;")); ASSERT_EQUALS("; x [ y - 1 ] = x [ y - 1 ] + 1 ;", tokenizeAndStringify("; x[y-1] += 1;"));
ASSERT_EQUALS("; x [ y ] = x [ y ++ ] + 1 ;", tokenizeAndStringify("; x[y++] += 1;"));
ASSERT_EQUALS("; x [ ++ y ] = x [ y ] + 1 ;", tokenizeAndStringify("; x[++y] += 1;"));
ASSERT_EQUALS(";", tokenizeAndStringify(";x += 0;")); ASSERT_EQUALS(";", tokenizeAndStringify(";x += 0;"));
ASSERT_EQUALS(";", tokenizeAndStringify(";x += '\\0';")); ASSERT_EQUALS(";", tokenizeAndStringify(";x += '\\0';"));