Fix crash when division by zero/modulo by zero happen on vectors

This commit is contained in:
Chaosus 2024-08-05 22:36:55 +03:00
parent 03afb92efa
commit ce265d9575
17 changed files with 127 additions and 2 deletions

View File

@ -585,8 +585,61 @@ void GDScriptByteCodeGenerator::write_unary_operator(const Address &p_target, Va
} }
void GDScriptByteCodeGenerator::write_binary_operator(const Address &p_target, Variant::Operator p_operator, const Address &p_left_operand, const Address &p_right_operand) { void GDScriptByteCodeGenerator::write_binary_operator(const Address &p_target, Variant::Operator p_operator, const Address &p_left_operand, const Address &p_right_operand) {
// Avoid validated evaluator for modulo and division when operands are int, since there's no check for division by zero. // Avoid validated evaluator for modulo and division when operands are int or integer vector, since there's no check for division by zero.
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)) { bool valid = HAS_BUILTIN_TYPE(p_left_operand) && HAS_BUILTIN_TYPE(p_right_operand);
if (valid && (p_operator == Variant::OP_DIVIDE || p_operator == Variant::OP_MODULE)) {
switch (p_left_operand.type.builtin_type) {
case Variant::INT:
switch (p_right_operand.type.builtin_type) {
case Variant::INT:
valid = false;
break;
default:
break;
}
break;
case Variant::VECTOR2I:
switch (p_right_operand.type.builtin_type) {
case Variant::INT:
valid = false;
break;
case Variant::VECTOR2I:
valid = false;
break;
default:
break;
}
break;
case Variant::VECTOR3I:
switch (p_right_operand.type.builtin_type) {
case Variant::INT:
valid = false;
break;
case Variant::VECTOR3I:
valid = false;
break;
default:
break;
}
break;
case Variant::VECTOR4I:
switch (p_right_operand.type.builtin_type) {
case Variant::INT:
valid = false;
break;
case Variant::VECTOR4I:
valid = false;
break;
default:
break;
}
break;
default:
break;
}
}
if (valid) {
if (p_target.mode == Address::TEMPORARY) { if (p_target.mode == Address::TEMPORARY) {
Variant::Type result_type = Variant::get_operator_return_type(p_operator, p_left_operand.type.builtin_type, p_right_operand.type.builtin_type); Variant::Type result_type = Variant::get_operator_return_type(p_operator, p_left_operand.type.builtin_type, p_right_operand.type.builtin_type);
Variant::Type temp_type = temporaries[p_target.address].type; Variant::Type temp_type = temporaries[p_target.address].type;

View File

@ -0,0 +1,3 @@
func test():
var integer = 1
integer /= 0

View File

@ -0,0 +1,6 @@
GDTEST_RUNTIME_ERROR
>> SCRIPT ERROR
>> on function: test()
>> runtime/errors/op_by_zero/division_by_zero_int.gd
>> 3
>> Division by zero error in operator '/'.

View File

@ -0,0 +1,3 @@
func test():
var vec2i = Vector2i(1, 1)
vec2i /= Vector2i(0, 0)

View File

@ -0,0 +1,6 @@
GDTEST_RUNTIME_ERROR
>> SCRIPT ERROR
>> on function: test()
>> runtime/errors/op_by_zero/division_by_zero_vector2i.gd
>> 3
>> Division by zero error in operator '/'.

View File

@ -0,0 +1,3 @@
func test():
var vec3i = Vector3i(1, 1, 1)
vec3i /= Vector3i(0, 0, 0)

View File

@ -0,0 +1,6 @@
GDTEST_RUNTIME_ERROR
>> SCRIPT ERROR
>> on function: test()
>> runtime/errors/op_by_zero/division_by_zero_vector3i.gd
>> 3
>> Division by zero error in operator '/'.

View File

@ -0,0 +1,3 @@
func test():
var vec4i = Vector4i(1, 1, 1, 1)
vec4i /= Vector4i(0, 0, 0, 0)

View File

@ -0,0 +1,6 @@
GDTEST_RUNTIME_ERROR
>> SCRIPT ERROR
>> on function: test()
>> runtime/errors/op_by_zero/division_by_zero_vector4i.gd
>> 3
>> Division by zero error in operator '/'.

View File

@ -0,0 +1,3 @@
func test():
var integer = 1
integer %= 0

View File

@ -0,0 +1,6 @@
GDTEST_RUNTIME_ERROR
>> SCRIPT ERROR
>> on function: test()
>> runtime/errors/op_by_zero/modulo_by_zero_int.gd
>> 3
>> Modulo by zero error in operator '%'.

View File

@ -0,0 +1,3 @@
func test():
var vec2i = Vector2i(1, 1)
vec2i %= Vector2i(0, 0)

View File

@ -0,0 +1,6 @@
GDTEST_RUNTIME_ERROR
>> SCRIPT ERROR
>> on function: test()
>> runtime/errors/op_by_zero/modulo_by_zero_vector2i.gd
>> 3
>> Modulo by zero error in operator '%'.

View File

@ -0,0 +1,3 @@
func test():
var vec3i = Vector3i(1, 1, 1)
vec3i %= Vector3i(0, 0, 0)

View File

@ -0,0 +1,6 @@
GDTEST_RUNTIME_ERROR
>> SCRIPT ERROR
>> on function: test()
>> runtime/errors/op_by_zero/modulo_by_zero_vector3i.gd
>> 3
>> Modulo by zero error in operator '%'.

View File

@ -0,0 +1,3 @@
func test():
var vec4i = Vector4i(1, 1, 1, 1)
vec4i %= Vector4i(0, 0, 0, 0)

View File

@ -0,0 +1,6 @@
GDTEST_RUNTIME_ERROR
>> SCRIPT ERROR
>> on function: test()
>> runtime/errors/op_by_zero/modulo_by_zero_vector4i.gd
>> 3
>> Modulo by zero error in operator '%'.