From f10634c021a8b173cea5e0eb6030fcd9e6cbef58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 23 Aug 2017 17:52:28 +0200 Subject: [PATCH] ValueFlow: Dont try to evaluate '/=0;' --- lib/valueflow.cpp | 22 ++++++++++++++++------ test/testvalueflow.cpp | 7 +++++++ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 143b28ed4..4e9dc83a1 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -1684,17 +1684,24 @@ static bool valueFlowForward(Token * const startToken, // Erase values that are not int values.. for (it = values.begin(); it != values.end();) { if (it->isIntValue()) { + bool ub = false; if (assign == "+=") it->intvalue += rhsValue.intvalue; else if (assign == "-=") it->intvalue -= rhsValue.intvalue; else if (assign == "*=") it->intvalue *= rhsValue.intvalue; - else if (assign == "/=") - it->intvalue /= rhsValue.intvalue; - else if (assign == "%=") - it->intvalue %= rhsValue.intvalue; - else if (assign == "&=") + else if (assign == "/=") { + if (rhsValue.intvalue == 0) + ub = true; + else + it->intvalue /= rhsValue.intvalue; + } else if (assign == "%=") { + if (rhsValue.intvalue == 0) + ub = true; + else + it->intvalue %= rhsValue.intvalue; + } else if (assign == "&=") it->intvalue &= rhsValue.intvalue; else if (assign == "|=") it->intvalue |= rhsValue.intvalue; @@ -1704,7 +1711,10 @@ static bool valueFlowForward(Token * const startToken, values.clear(); break; } - ++it; + if (ub) + it = values.erase(it); + else + ++it; } else if (it->isFloatValue()) { if (assign == "+=") it->floatValue += rhsValue.intvalue; diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index a563edf8a..6c598a1b2 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -1746,6 +1746,13 @@ private: "}"; ASSERT_EQUALS(true, testValueOfX(code, 4U, 166)); + code = "void f() {\n" + " int x = 123;\n" + " x /= 0;\n" // don't crash when evaluating x/=0 + " return x;\n" + "}"; + ASSERT_EQUALS(false, testValueOfX(code, 4U, 123)); + code = "void f() {\n" " float x = 123.45;\n" " x += 67;\n"