From eb3bf571beb317d6b80038e047497dcf4278c379 Mon Sep 17 00:00:00 2001 From: Simon Martin Date: Sun, 28 Jul 2013 12:32:18 +0200 Subject: [PATCH] Avoid divisions by zero when simplifying numeric calculations. --- lib/templatesimplifier.cpp | 2 +- test/testother.cpp | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/templatesimplifier.cpp b/lib/templatesimplifier.cpp index e63d8e8f6..a90612c84 100644 --- a/lib/templatesimplifier.cpp +++ b/lib/templatesimplifier.cpp @@ -766,7 +766,7 @@ bool TemplateSimplifier::simplifyNumericCalculations(Token *tok) while (tok->tokAt(4) && tok->next()->isNumber() && tok->tokAt(3)->isNumber()) { // %any% %num% %any% %num% %any% const Token* op = tok->tokAt(2); const Token* after = tok->tokAt(4); - if (Token::Match(tok, "* %num% /") && tok->next()->str() == MathLib::multiply(tok->strAt(3), MathLib::divide(tok->next()->str(), tok->strAt(3)))) { + if (Token::Match(tok, "* %num% /") && (tok->strAt(3) != "0") && tok->next()->str() == MathLib::multiply(tok->strAt(3), MathLib::divide(tok->next()->str(), tok->strAt(3)))) { // Division where result is a whole number } else if (!((op->str() == "*" && (isLowerThanMulDiv(tok) || tok->str() == "*") && isLowerEqualThanMulDiv(after)) || // associative (Token::Match(op, "[/%]") && isLowerThanMulDiv(tok) && isLowerEqualThanMulDiv(after)) || // NOT associative diff --git a/test/testother.cpp b/test/testother.cpp index 4e1d99c6a..3cdc46f07 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -42,6 +42,7 @@ private: TEST_CASE(zeroDiv4); TEST_CASE(zeroDiv5); TEST_CASE(zeroDiv6); + TEST_CASE(zeroDiv7); // #4930 TEST_CASE(nanInArithmeticExpression); @@ -414,6 +415,15 @@ private: "} } }"); ASSERT_EQUALS("[test.cpp:3]: (error) Division by zero.\n", errout.str()); } + + void zeroDiv7() { + check("void f() {\n" + " int a = 1/2*3/0;\n" + " int b = 1/2*3%0;\n" + "}"); + ASSERT_EQUALS("[test.cpp:2]: (error) Division by zero.\n" + "[test.cpp:3]: (error) Division by zero.\n", errout.str()); + } void nanInArithmeticExpression() { check("void f()\n"