diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 4f6b472b3..fc0cf00d2 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1609,7 +1609,9 @@ void CheckOther::checkZeroDivision() const bool printInconclusive = _settings->inconclusive; for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { - if (!Token::Match(tok, "[/%]") || !tok->astOperand1() || !tok->astOperand2()) + if (!tok->astOperand2() || !tok->astOperand1()) + continue; + if (tok->str() != "%" && tok->str() != "/" && tok->str() != "%=" && tok->str() != "/=") continue; if (!tok->valueType() || !tok->valueType()->isIntegral()) continue; @@ -1619,9 +1621,9 @@ void CheckOther::checkZeroDivision() } else if (tok->astOperand1()->isName()) { if (tok->astOperand1()->variable() && !tok->astOperand1()->variable()->isIntegralType()) continue; - } else if (!tok->astOperand1()->isArithmeticalOp()) { + } else if (!tok->astOperand1()->isArithmeticalOp()) continue; - } + // Value flow.. const ValueFlow::Value *value = tok->astOperand2()->getValue(0LL); if (!value) diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index 3915b283e..ddbd7924e 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -3720,7 +3720,7 @@ static void setValueType(Token *tok, const ValueType &valuetype, bool cpp, Value if (vt2 && vt1->isIntegral() && vt1->pointer == 0U && vt2->isIntegral() && vt2->pointer == 0U && - (parent->isArithmeticalOp() ||parent->tokType() == Token::eBitOp)) { + (parent->isArithmeticalOp() || parent->tokType() == Token::eBitOp || parent->isAssignmentOp())) { ValueType vt; if (vt1->type == vt2->type) { diff --git a/test/testother.cpp b/test/testother.cpp index f0def4c2c..894f79c49 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(zeroDiv4); TEST_CASE(zeroDiv5); TEST_CASE(zeroDiv6); @@ -293,6 +294,28 @@ private: ASSERT_EQUALS("", errout.str()); } + void zeroDiv3() { + check("int foo(int i) {\n" + " return i / 0;\n" + "}"); + ASSERT_EQUALS("[test.cpp:2]: (error) Division by zero.\n", errout.str()); + + check("int foo(int i) {\n" + " return i % 0;\n" + "}"); + ASSERT_EQUALS("[test.cpp:2]: (error) Division by zero.\n", errout.str()); + + check("void foo(int& i) {\n" + " i /= 0;\n" + "}"); + ASSERT_EQUALS("[test.cpp:2]: (error) Division by zero.\n", errout.str()); + + check("void foo(int& i) {\n" + " i %= 0;\n" + "}"); + ASSERT_EQUALS("[test.cpp:2]: (error) Division by zero.\n", errout.str()); + } + void zeroDiv4() { check("void f()\n" "{\n"