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:
parent
c9449f9bd0
commit
0d468d97c7
|
@ -1218,12 +1218,15 @@ 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::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);
|
zerodivError(tok);
|
||||||
}
|
}
|
||||||
else if (Token::Match(tok, "div|ldiv|lldiv|imaxdiv ( %num% , %num% )") &&
|
else if (Token::Match(tok, "div|ldiv|lldiv|imaxdiv ( %num% , %num% )")
|
||||||
tok->tokAt(4)->str()[0] == '0')
|
&& MathLib::toLongNumber(tok->tokAt(4)->str()) == 0L
|
||||||
|
&& MathLib::toDoubleNumber(tok->tokAt(4)->str()) == 0.0)
|
||||||
{
|
{
|
||||||
zerodivError(tok);
|
zerodivError(tok);
|
||||||
}
|
}
|
||||||
|
|
|
@ -173,6 +173,30 @@ private:
|
||||||
" div_t divresult = div (1,0x5);\n"
|
" div_t divresult = div (1,0x5);\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("", errout.str());
|
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()
|
void delete1()
|
||||||
|
|
Loading…
Reference in New Issue