From 027e5cc860647c299986aff8eb71fe6850fb854d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 22 Sep 2013 09:52:12 +0200 Subject: [PATCH] Fixed #4929 (false positive: Division by zero (standard function div())) --- lib/checkother.cpp | 6 +++--- test/testother.cpp | 31 ++++++++++++++++++------------- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 71d87c7ee..88f578f0b 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -2142,9 +2142,9 @@ void CheckOther::checkZeroDivision() MathLib::isInt(tok->next()->str()) && MathLib::toLongNumber(tok->next()->str()) == 0L) { zerodivError(tok); - } else if (Token::Match(tok, "div|ldiv|lldiv|imaxdiv ( %num% , %num% )") && - MathLib::isInt(tok->strAt(4)) && - MathLib::toLongNumber(tok->strAt(4)) == 0L) { + } else if (Token::Match(tok, "std :: div|ldiv|lldiv|imaxdiv ( %num% , %num% )") && + MathLib::isInt(tok->strAt(6)) && + MathLib::toLongNumber(tok->strAt(6)) == 0L) { zerodivError(tok); } } diff --git a/test/testother.cpp b/test/testother.cpp index 0a82c6567..8512ffd52 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -336,9 +336,26 @@ private: void zeroDiv3() { check("void f()\n" "{\n" - " div_t divresult = div (1,0);\n" + " div_t divresult = std::div (1,0);\n" "}"); ASSERT_EQUALS("[test.cpp:3]: (error) Division by zero.\n", errout.str()); + + check("void f()\n" + "{\n" + " div_t divresult = std::div (1,0L);\n" + "}"); + ASSERT_EQUALS("[test.cpp:3]: (error) Division by zero.\n", errout.str()); + + check("void f()\n" + "{\n" + " div_t divresult = std::div (1,0x5);\n" + "}"); + ASSERT_EQUALS("", errout.str()); + + check("void f() {\n" // #4929 - we don't know if this "div" is the standard function + " div (1,0);\n" + "}"); + ASSERT_EQUALS("", errout.str()); } void zeroDiv4() { @@ -366,18 +383,6 @@ private: "}"); ASSERT_EQUALS("[test.cpp:3]: (error) Division by zero.\n", errout.str()); - check("void f()\n" - "{\n" - " div_t divresult = div (1,0L);\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" - "}"); - ASSERT_EQUALS("", errout.str()); - // Don't warn about floating points (gcc doesn't warn either) // and floating points are handled differently than integers. check("void f()\n"