Skip to content

Commit 0524e29

Browse files
committed
Fix crash when division by zero/modulo by zero happen on vectors
1 parent 9e60984 commit 0524e29

File tree

5 files changed

+37
-2
lines changed

5 files changed

+37
-2
lines changed

modules/gdscript/gdscript_byte_codegen.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -585,8 +585,25 @@ void GDScriptByteCodeGenerator::write_unary_operator(const Address &p_target, Va
585585
}
586586

587587
void GDScriptByteCodeGenerator::write_binary_operator(const Address &p_target, Variant::Operator p_operator, const Address &p_left_operand, const Address &p_right_operand) {
588-
// Avoid validated evaluator for modulo and division when operands are int, since there's no check for division by zero.
589-
if (HAS_BUILTIN_TYPE(p_left_operand) && HAS_BUILTIN_TYPE(p_right_operand) && ((p_operator != Variant::OP_DIVIDE && p_operator != Variant::OP_MODULE) || p_left_operand.type.builtin_type != Variant::INT || p_right_operand.type.builtin_type != Variant::INT)) {
588+
bool valid = HAS_BUILTIN_TYPE(p_left_operand) && HAS_BUILTIN_TYPE(p_right_operand);
589+
590+
// Avoid validated evaluator for modulo and division when operands are int or integer vector, since there's no check for division by zero.
591+
if (valid && (p_operator == Variant::OP_DIVIDE || p_operator == Variant::OP_MODULE)) {
592+
switch (p_left_operand.type.builtin_type) {
593+
case Variant::INT:
594+
valid = p_right_operand.type.builtin_type != Variant::INT;
595+
break;
596+
case Variant::VECTOR2I:
597+
case Variant::VECTOR3I:
598+
case Variant::VECTOR4I:
599+
valid = p_right_operand.type.builtin_type != Variant::INT && p_right_operand.type.builtin_type != p_left_operand.type.builtin_type;
600+
break;
601+
default:
602+
break;
603+
}
604+
}
605+
606+
if (valid) {
590607
if (p_target.mode == Address::TEMPORARY) {
591608
Variant::Type result_type = Variant::get_operator_return_type(p_operator, p_left_operand.type.builtin_type, p_right_operand.type.builtin_type);
592609
Variant::Type temp_type = temporaries[p_target.address].type;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
func test():
2+
var integer: int = 1
3+
integer /= 0
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
GDTEST_RUNTIME_ERROR
2+
>> SCRIPT ERROR
3+
>> on function: test()
4+
>> runtime/errors/division_by_zero.gd
5+
>> 3
6+
>> Division by zero error in operator '/'.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
func test():
2+
var integer: int = 1
3+
integer %= 0
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
GDTEST_RUNTIME_ERROR
2+
>> SCRIPT ERROR
3+
>> on function: test()
4+
>> runtime/errors/modulo_by_zero.gd
5+
>> 3
6+
>> Modulo by zero error in operator '%'.

0 commit comments

Comments
 (0)