From be6daa94bbeb1bb5c10b9aa2a13712f631593d76 Mon Sep 17 00:00:00 2001 From: Paul Fultz II Date: Mon, 9 May 2022 13:27:32 -0500 Subject: [PATCH] Fix 11053: Integer division by zero in calculate.h (#4092) * Fix 11053: Integer division by zero in calculate.h * Format --- lib/calculate.h | 2 +- test/testvalueflow.cpp | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/calculate.h b/lib/calculate.h index 663d2939a..3e62e212b 100644 --- a/lib/calculate.h +++ b/lib/calculate.h @@ -69,7 +69,7 @@ R calculate(const std::string& s, const T& x, const T& y, bool* error = nullptr) } return wrap(x / y); case '%': - if (isZero(y)) { + if (isZero(MathLib::bigint(y))) { if (error) *error = true; return R{}; diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index e729a7b6b..0760df72a 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -6519,6 +6519,20 @@ private: code = "void f(const char * const x) { !!system(x); }\n"; valueOfTok(code, "x"); + + code = "void setDeltas(int life, int age, int multiplier) {\n" + " int dx = 0;\n" + " int dy = 0;\n" + " if (age <= 2 || life < 4) {\n" + " dy = 0;\n" + " dx = (rand() % 3) - 1;\n" + " }\n" + " else if (age < (multiplier * 3)) {\n" + " if (age % (int) (multiplier * 0.5) == 0) dy = -1;\n" + " else dy = 0;\n" + " }\n" + "}\n"; + valueOfTok(code, "age"); } void valueFlowHang() {