Fix #630 (Division by zero check only looks at first character of divisor)

http://sourceforge.net/apps/trac/cppcheck/ticket/630
Thanks to liam_routt for finding this and providing fix for it.
This commit is contained in:
Reijo Tomperi 2009-09-01 17:00:26 +03:00
parent c9449f9bd0
commit 0d468d97c7
2 changed files with 30 additions and 3 deletions

View File

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

View File

@ -173,6 +173,30 @@ private:
" div_t divresult = div (1,0x5);\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("void f()\n"
"{\n"
" long a = b / 0.0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (error) Division by zero\n", errout.str());
check("void f()\n"
"{\n"
" long a = b / 0.5;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("void f()\n"
"{\n"
" div_t divresult = div (1,0.0);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (error) Division by zero\n", errout.str());
check("void f()\n"
"{\n"
" div_t divresult = div (1,0.5);\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void delete1()