From e6120a5725593285000ac01b9465ff4740e53750 Mon Sep 17 00:00:00 2001 From: Slava Semushin Date: Sun, 23 Aug 2009 10:34:19 +0700 Subject: [PATCH] Fixed #581 (Wrong usage of div-function) http://sourceforge.net/apps/trac/cppcheck/ticket/581 --- src/checkother.cpp | 6 ++++++ test/testother.cpp | 10 ++++++++++ 2 files changed, 16 insertions(+) diff --git a/src/checkother.cpp b/src/checkother.cpp index 32881bff1..7ac8010a6 100644 --- a/src/checkother.cpp +++ b/src/checkother.cpp @@ -1219,7 +1219,13 @@ void CheckOther::checkZeroDivision() for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { if (Token::simpleMatch(tok, "/ 0")) + { zerodivError(tok); + } + else if (Token::Match(tok, "div|ldiv|lldiv|imaxdiv ( %num% , 0 )")) + { + zerodivError(tok); + } } } diff --git a/test/testother.cpp b/test/testother.cpp index 3bd768c8d..a7ad6ce00 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -36,6 +36,7 @@ private: { TEST_CASE(zeroDiv1); TEST_CASE(zeroDiv2); + TEST_CASE(zeroDiv3); TEST_CASE(delete1); TEST_CASE(delete2); @@ -125,6 +126,15 @@ private: ASSERT_EQUALS("", errout.str()); } + void zeroDiv3() + { + check("void f()\n" + "{\n" + " div_t divresult = div (1,0);\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:3]: (error) Division by zero\n", errout.str()); + } + void delete1() { check("void foo()\n"