Browse Source

Math: Prevent division by zero in posmod

Fixes #43932.

Co-authored-by: David Hoppenbrouwers <[email protected]>
Rémi Verschelde 2 years ago
parent
commit
f011d8ca9c
1 changed files with 2 additions and 0 deletions
  1. 2 0
      core/math/math_funcs.h

+ 2 - 0
core/math/math_funcs.h

@@ -31,6 +31,7 @@
 #ifndef MATH_FUNCS_H
 #define MATH_FUNCS_H
 
+#include "core/error/error_macros.h"
 #include "core/math/math_defs.h"
 #include "core/math/random_pcg.h"
 #include "core/typedefs.h"
@@ -225,6 +226,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;