Fixed #1740 (Undefined Behavior: Divide by zero)

This commit is contained in:
Philipp Kloke 2011-09-10 16:12:53 +02:00 committed by Daniel Marjamäki
parent 08efad13b2
commit 2f62d180fe
2 changed files with 11 additions and 2 deletions

View File

@ -1870,8 +1870,7 @@ void CheckOther::checkZeroDivision()
{
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
{
if (Token::Match(tok, "/ %num%") &&
if (Token::Match(tok, "[/%] %num%") &&
MathLib::isInt(tok->next()->str()) &&
MathLib::toLongNumber(tok->next()->str()) == 0L)
{

View File

@ -42,6 +42,7 @@ private:
TEST_CASE(zeroDiv3);
TEST_CASE(zeroDiv4);
TEST_CASE(zeroDiv5);
TEST_CASE(zeroDiv6);
TEST_CASE(sprintf1); // Dangerous usage of sprintf
TEST_CASE(sprintf2);
@ -367,6 +368,15 @@ private:
ASSERT_EQUALS("[test.cpp:3]: (error) Division by zero\n", errout.str());
}
void zeroDiv6()
{
check("void f()\n"
"{ { {\n"
" int a = b % 0;\n"
"} } }\n");
ASSERT_EQUALS("[test.cpp:3]: (error) Division by zero\n", errout.str());
}
void sprintfUsage(const char code[])
{