Math: Prevent division by zero in posmod

Fixes #43932.

Co-authored-by: David Hoppenbrouwers <david@salt-inc.org>
(cherry picked from commit f011d8ca9c)
This commit is contained in:
Rémi Verschelde 2023-02-11 20:44:45 +01:00
parent a836b6bc88
commit 5faa423944
No known key found for this signature in database
GPG key ID: C3336907360768E1

View file

@ -31,6 +31,7 @@
#ifndef MATH_FUNCS_H #ifndef MATH_FUNCS_H
#define MATH_FUNCS_H #define MATH_FUNCS_H
#include "core/error_macros.h"
#include "core/math/math_defs.h" #include "core/math/math_defs.h"
#include "core/math/random_pcg.h" #include "core/math/random_pcg.h"
#include "core/typedefs.h" #include "core/typedefs.h"
@ -200,6 +201,7 @@ public:
return value; return value;
} }
static _ALWAYS_INLINE_ int64_t posmod(int64_t p_x, int64_t p_y) { 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; int64_t value = p_x % p_y;
if (((value < 0) && (p_y > 0)) || ((value > 0) && (p_y < 0))) { if (((value < 0) && (p_y > 0)) || ((value > 0) && (p_y < 0))) {
value += p_y; value += p_y;