Fixed #1614 (negative array index issues in latest from git)

This commit is contained in:
Daniel Marjamäki 2010-04-20 16:43:51 +02:00
parent e8ac1f07d9
commit 7e2f39290d
2 changed files with 18 additions and 1 deletions

View File

@ -1280,7 +1280,17 @@ void CheckBufferOverrun::negativeIndex()
const long index = MathLib::toLongNumber(tok->next()->str());
if (index < 0)
{
negativeIndexError(tok, index);
// Multidimension index => error
if (Token::simpleMatch(tok->previous(), "]") || Token::simpleMatch(tok->tokAt(3), "["))
negativeIndexError(tok, index);
// 1-dimensional array => error
else if (tok->previous() && tok->previous()->varId())
{
const Token *tok2 = Token::findmatch(_tokenizer->tokens(), "%varid%", tok->previous()->varId());
if (tok2 && Token::Match(tok2->next(), "[ %any% ] ;"))
negativeIndexError(tok, index);
}
}
}
}

View File

@ -1022,6 +1022,13 @@ private:
" data[5][-1] = 0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (error) Array index -1 corresponds with 4294967295, which is likely out of bounds\n", errout.str());
// #1614 - negative index is ok for pointers
check("void foo(char *p)\n"
"{\n"
" p[-1] = 0;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void array_index_for_decr()