Fixed #1850 (An access to a nested std::map via a negative integer key is reported as 'Array index out of bounds')

This commit is contained in:
Daniel Marjamäki 2010-07-14 12:24:07 +02:00
parent 233432a888
commit bea714445a
2 changed files with 14 additions and 7 deletions

View File

@ -1596,15 +1596,15 @@ void CheckBufferOverrun::negativeIndex()
const long index = MathLib::toLongNumber(tok->next()->str());
if (index < 0)
{
// Multidimensional index => error
if (Token::simpleMatch(tok->previous(), "]") || Token::simpleMatch(tok->tokAt(3), "["))
negativeIndexError(tok, index);
// Negative index. Check if it's an array.
const Token *tok2 = tok;
while (Token::simpleMatch(tok2->previous(), "]"))
tok2 = tok2->previous()->link();
// 1-dimensional array => error
else if (tok->previous() && tok->previous()->varId())
if (tok2->previous() && tok2->previous()->varId())
{
const Token *tok2 = Token::findmatch(_tokenizer->tokens(), "%varid%", tok->previous()->varId());
if (tok2 && Token::Match(tok2->next(), "[ %any% ] ;"))
const Token *tok3 = Token::findmatch(_tokenizer->tokens(), "%varid%", tok2->previous()->varId());
if (tok3 && Token::Match(tok3->next(), "[ %any% ] ;|["))
negativeIndexError(tok, index);
}
}

View File

@ -1155,6 +1155,13 @@ private:
" if (p[-1]);\n"
"}\n");
ASSERT_EQUALS("", errout.str());
// ticket #1850
check("int f(const std::map<int, std::map<int,int> > &m)\n"
"{\n"
" return m[0][-1];\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void array_index_for_decr()