Fixed #1695 (Ticket #1614 is broken using latest from git)

This commit is contained in:
Daniel Marjamäki 2010-05-19 19:23:09 +02:00
parent 6815f4e941
commit c31accc52a
2 changed files with 21 additions and 23 deletions

View File

@ -48,17 +48,17 @@ CheckBufferOverrun instance;
void CheckBufferOverrun::arrayIndexOutOfBounds(const Token *tok, int size, int index)
{
if (size == 1)
return;
std::ostringstream errmsg;
errmsg << "Array '";
if (tok)
errmsg << tok->str();
else
errmsg << "array";
errmsg << "[" << size << "]' index " << index << " out of bounds";
reportError(tok, Severity::error, "arrayIndexOutOfBounds", errmsg.str().c_str());
if (size > 1)
{
std::ostringstream errmsg;
errmsg << "Array '";
if (tok)
errmsg << tok->str();
else
errmsg << "array";
errmsg << "[" << size << "]' index " << index << " out of bounds";
reportError(tok, Severity::error, "arrayIndexOutOfBounds", errmsg.str().c_str());
}
}
void CheckBufferOverrun::arrayIndexOutOfBounds(const Token *tok, const ArrayInfo &arrayInfo, const std::vector<int> &index)
@ -780,17 +780,6 @@ void CheckBufferOverrun::checkScope(const Token *tok, const ArrayInfo &arrayInfo
}
// in case %var% is declared as a pointer
else if (Token::Match(tok, "%var% [ %num% ]"))
{
const int index = MathLib::toLongNumber(tok->strAt(2));
if (index < 0)
{
arrayIndexOutOfBounds(tok, index, index);
}
}
// Loop..
else if (Token::simpleMatch(tok, "for ("))
{

View File

@ -943,7 +943,8 @@ private:
" int *ip = &i[1];\n"
" ip[-10] = 1;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5]: (error) Array 'ip[-10]' index -10 out of bounds\n", errout.str());
ASSERT_EQUALS("", errout.str());
TODO_ASSERT_EQUALS("[test.cpp:5]: (error) Array ip[-10] out of bounds\n", errout.str());
}
void array_index_multidim()
@ -1118,6 +1119,14 @@ private:
" p[-1] = 0;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("void foo()\n"
"{\n"
" char s[] = \"abc\";\n"
" char *p = s + strlen(s);\n"
" if (p[-1]);\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void array_index_for_decr()