5402 false positive: (error) Division by zero -- with boost::format

This commit is contained in:
Alexander Mai 2015-08-01 17:28:54 +02:00
parent 1741af497f
commit f91ad9bbf6
2 changed files with 23 additions and 1 deletions

View File

@ -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)

View File

@ -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"