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