diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 42c3af4b7..16a4a8e8d 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -679,6 +679,8 @@ static void execute(const Token *expr, *result = result1 - result2; else if (expr->str() == "*") *result = result1 * result2; + else if (result2 == 0) + *error = true; else if (expr->str() == "/") *result = result1 / result2; else if (expr->str() == "%") diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index e22a83784..2329398a7 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -641,6 +641,12 @@ private: ASSERT_EQUALS(true, testValueOfX(code, 3U, 8)); ASSERT_EQUALS(false, testValueOfX(code, 3U, 10)); + code = "void f() {\n" + " for (int x = 0; x < 10; x = x / 0)\n" + " a[x] = 0;\n" + "}"; + testValueOfX(code, 3U, 0); // don't crash + code = "void f() {\n" " for (int x = 0; x < 10; x++)\n" " x<4 ?\n"