From f3e1aab267ebd7e265896a51e4a5a08048632e52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Verschelde?= Date: Sat, 11 Feb 2023 20:44:45 +0100 Subject: [PATCH] Math: Prevent division by zero in posmod Fixes #43932. Co-authored-by: David Hoppenbrouwers (cherry picked from commit f011d8ca9ca25232fb335eead1c8eeaf5c7f2c54) --- core/math/math_funcs.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/core/math/math_funcs.h b/core/math/math_funcs.h index f545f137e..d807d4fc0 100644 --- a/core/math/math_funcs.h +++ b/core/math/math_funcs.h @@ -30,6 +30,7 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ +#include "core/error_macros.h" #include "core/math/math_defs.h" #include "core/math/random_pcg.h" #include "core/typedefs.h" @@ -236,6 +237,7 @@ public: } static _ALWAYS_INLINE_ int64_t posmod(int64_t p_x, int64_t p_y) { + ERR_FAIL_COND_V_MSG(p_y == 0, 0, "Division by zero in posmod is undefined. Returning 0 as fallback."); int64_t value = p_x % p_y; if (((value < 0) && (p_y > 0)) || ((value > 0) && (p_y < 0))) { value += p_y;