From c14a3d67bbbdd44d8c8cad8ab6e71948ecef4604 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 23 Mar 2014 20:37:56 +0100 Subject: [PATCH] ValueFlow: Handle division by zero better in abstract interpretation --- lib/valueflow.cpp | 2 ++ test/testvalueflow.cpp | 6 ++++++ 2 files changed, 8 insertions(+) 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"