Fixed #581 (Wrong usage of div-function)

http://sourceforge.net/apps/trac/cppcheck/ticket/581
This commit is contained in:
Slava Semushin 2009-08-23 10:34:19 +07:00
parent 703fd45f78
commit e6120a5725
2 changed files with 16 additions and 0 deletions

View File

@ -1219,7 +1219,13 @@ void CheckOther::checkZeroDivision()
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
{ {
if (Token::simpleMatch(tok, "/ 0")) if (Token::simpleMatch(tok, "/ 0"))
{
zerodivError(tok); zerodivError(tok);
}
else if (Token::Match(tok, "div|ldiv|lldiv|imaxdiv ( %num% , 0 )"))
{
zerodivError(tok);
}
} }
} }

View File

@ -36,6 +36,7 @@ private:
{ {
TEST_CASE(zeroDiv1); TEST_CASE(zeroDiv1);
TEST_CASE(zeroDiv2); TEST_CASE(zeroDiv2);
TEST_CASE(zeroDiv3);
TEST_CASE(delete1); TEST_CASE(delete1);
TEST_CASE(delete2); TEST_CASE(delete2);
@ -125,6 +126,15 @@ private:
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void zeroDiv3()
{
check("void f()\n"
"{\n"
" div_t divresult = div (1,0);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (error) Division by zero\n", errout.str());
}
void delete1() void delete1()
{ {
check("void foo()\n" check("void foo()\n"