Fix ticket #612 (Division by zero not detected when zero has type suffix)

http://sourceforge.net/apps/trac/cppcheck/ticket/612
This commit is contained in:
Reijo Tomperi 2009-08-30 22:02:09 +03:00
parent 9ede0e26ab
commit 6fb44e733b
2 changed files with 43 additions and 2 deletions

View File

@ -1218,11 +1218,12 @@ void CheckOther::checkZeroDivision()
{
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
{
if (Token::simpleMatch(tok, "/ 0"))
if (Token::Match(tok, "/ %num%") && tok->next()->str()[0] == '0')
{
zerodivError(tok);
}
else if (Token::Match(tok, "div|ldiv|lldiv|imaxdiv ( %num% , 0 )"))
else if (Token::Match(tok, "div|ldiv|lldiv|imaxdiv ( %num% , %num% )") &&
tok->tokAt(4)->str()[0] == '0')
{
zerodivError(tok);
}

View File

@ -37,6 +37,7 @@ private:
TEST_CASE(zeroDiv1);
TEST_CASE(zeroDiv2);
TEST_CASE(zeroDiv3);
TEST_CASE(zeroDiv4);
TEST_CASE(delete1);
TEST_CASE(delete2);
@ -135,6 +136,45 @@ private:
ASSERT_EQUALS("[test.cpp:3]: (error) Division by zero\n", errout.str());
}
void zeroDiv4()
{
check("void f()\n"
"{\n"
" long a = b / 0x6;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("void f()\n"
"{\n"
" long a = b / 0x0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (error) Division by zero\n", errout.str());
check("void f()\n"
"{\n"
" long a = b / 0L;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (error) Division by zero\n", errout.str());
check("void f()\n"
"{\n"
" long a = b / 0ul;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (error) Division by zero\n", errout.str());
check("void f()\n"
"{\n"
" div_t divresult = div (1,0L);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (error) Division by zero\n", errout.str());
check("void f()\n"
"{\n"
" div_t divresult = div (1,0x5);\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void delete1()
{
check("void foo()\n"