diff --git a/lib/checkother.cpp b/lib/checkother.cpp index ae7dddc96..b796f8ee9 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1797,10 +1797,20 @@ void CheckOther::checkZeroDivision() const bool printInconclusive = _settings->inconclusive; for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { - if (!Token::Match(tok, "[/%]") || !tok->astOperand2()) + if (!Token::Match(tok, "[/%]") || !tok->astOperand1() || !tok->astOperand2()) continue; if (astIsFloat(tok,false)) continue; + if (tok->astOperand1()->isNumber()) { + if (MathLib::isFloat(tok->astOperand1()->str())) + continue; + } else if (tok->astOperand1()->isName()) { + if (tok->astOperand1()->variable() && !tok->astOperand1()->variable()->isIntegralType()) + continue; + } else { + continue; + } + // Value flow.. const ValueFlow::Value *value = tok->astOperand2()->getValue(0LL); if (!value) diff --git a/test/testother.cpp b/test/testother.cpp index 61e22debe..08f41cb09 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -42,6 +42,7 @@ private: TEST_CASE(zeroDiv7); // #4930 TEST_CASE(zeroDiv8); TEST_CASE(zeroDiv9); + TEST_CASE(zeroDiv10); TEST_CASE(zeroDivCond); // division by zero / useless condition @@ -376,6 +377,17 @@ private: ASSERT_EQUALS("", errout.str()); } + void zeroDiv10() { + // #5402 false positive: (error) Division by zero -- with boost::format + check("int main() {\n" + " std::cout\n" + " << boost::format(\" %d :: %s <> %s\") % 0 % \"a\" % \"b\"\n" + " << std::endl;\n" + " return 0;\n" + "}"); + ASSERT_EQUALS("", errout.str()); + } + void zeroDivCond() { check("void f(unsigned int x) {\n" " int y = 17 / x;\n"