diff --git a/src/checkother.cpp b/src/checkother.cpp index 7c3e55465..9aeeb02a5 100644 --- a/src/checkother.cpp +++ b/src/checkother.cpp @@ -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); } diff --git a/test/testother.cpp b/test/testother.cpp index 419a2942a..dc48fe925 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -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()