Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Division by zero crashes editor and builds instead of logging error message #83308

Closed
kebabskal opened this issue Oct 14, 2023 · 7 comments · Fixed by #83569
Closed

Division by zero crashes editor and builds instead of logging error message #83308

kebabskal opened this issue Oct 14, 2023 · 7 comments · Fixed by #83569

Comments

@kebabskal
Copy link

kebabskal commented Oct 14, 2023

Godot version

v4.2.beta1.official [b137180]

System information

Godot v4.2.beta1 - Windows 10.0.22621 - Vulkan (Forward+) - dedicated NVIDIA GeForce RTX 2070 (NVIDIA; 31.0.15.3640) - AMD Ryzen 7 5800X 8-Core Processor (16 Threads)

Issue description

When you are dividing ints by 0 builds and editor hard crashes.
In 4.1.2, both editor and builds, there's an error message logged.
(Editor crashes only happen when using @tool)

Floats doesn't crash, and doesn't log an error, instead it results in "inf".

The editor warns about division by zero if you try 1/0 etc.
Boxing the divisor in a variable yields no warning:

var zero:int = 0
1/zero	

Steps to reproduce

  1. Open the supplied minimal project in Godot 4.2 beta 1
  2. Press "Run Project"
  3. Observe the float example resulting in "inf" and then a hard crash when it tries to do the same with ints

Minimal reproduction project

div-by-zero-bug.zip

Edit: Fixed erroneous godot version. Got confused and copied from the wrong window.

@akien-mga
Copy link
Member

Godot version

Godot v4.1.2.stable

To clarify, I believe you meant to report this against 4.2-beta1.

I tested locally on Linux with 4.2-beta1 and it's not crashing for me:

image

Might be Windows specific.

@kebabskal
Copy link
Author

kebabskal commented Oct 14, 2023

To clarify, I believe you meant to report this against 4.2-beta1.

Good eye, sniper!
My bad. Updated the issue, copying from the right godot instance.

Might be Windows specific.

Does seem like it, yes. The other person replicating this over on chat.godotengine.org also ran Windows.

@Wierdox
Copy link
Contributor

Wierdox commented Oct 14, 2023

Oh right I forgot to post the console log for when I replicated the crash on rocket chat. Not sure if it's any use though. I could only get the backtrace when using my custom build of 4.2(probably my fault), but I got the same crash on the official.

Got this from var x :int= 5 % str_to_var("0")

================================================================
CrashHandlerException: Program crashed
Engine version: Godot Engine v4.2.beta.custom_build (7b33ad354a1884c027487892f2e44e8cc6862076)
Dumping the backtrace. Please include this when reporting the bug to the project developer.
[0] OperatorEvaluatorModNZ<__int64,__int64,__int64>::validated_evaluate (D:\godot\core\variant\variant_op.h:294)
[1] OperatorEvaluatorModNZ<__int64,__int64,__int64>::validated_evaluate (D:\godot\core\variant\variant_op.h:294)
[2] GDScriptFunction::call (D:\godot\modules\gdscript\gdscript_vm.cpp:686)
[3] GDScriptFunctionState::resume (D:\godot\modules\gdscript\gdscript_function.cpp:222)
[4] GDScriptFunctionState::_signal_callback (D:\godot\modules\gdscript\gdscript_function.cpp:166)
[5] MethodBindVarArgTR<GDScriptFunctionState,Variant>::call (D:\godot\core\object\method_bind.h:265)
[6] Object::callp (D:\godot\core\object\object.cpp:774)
[7] Callable::callp (D:\godot\core\variant\callable.cpp:69)
[8] CallableCustomBind::call (D:\godot\core\variant\callable_bind.cpp:145)
[9] Callable::callp (D:\godot\core\variant\callable.cpp:58)
[10] Object::emit_signalp (D:\godot\core\object\object.cpp:1129)
[11] Object::emit_signal<> (D:\godot\core\object\object.h:920)
[12] SceneTree::process_timers (D:\godot\scene\main\scene_tree.cpp:584)
[13] SceneTree::process (D:\godot\scene\main\scene_tree.cpp:520)
[14] Main::iteration (D:\godot\main\main.cpp:3603)
[15] OS_Windows::run (D:\godot\platform\windows\os_windows.cpp:1474)
[16] widechar_main (D:\godot\platform\windows\godot_windows.cpp:182)
[17] _main (D:\godot\platform\windows\godot_windows.cpp:206)
[18] main (D:\godot\platform\windows\godot_windows.cpp:218)
[19] __scrt_common_main_seh (D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:288)
[20] <couldn't map PC to fn name>
-- END OF BACKTRACE --
================================================================

Edit: forgot debug symbols.

@AThousandShips
Copy link
Member

Please build with debug symbols

@Wierdox
Copy link
Contributor

Wierdox commented Oct 14, 2023

Furthermore, backtrace of the repro:

float: div by zero: inf

================================================================
CrashHandlerException: Program crashed
Engine version: Godot Engine v4.2.beta.custom_build (7b33ad354a1884c027487892f2e44e8cc6862076)
Dumping the backtrace. Please include this when reporting the bug to the project developer.
[0] OperatorEvaluatorDivNZ<__int64,__int64,__int64>::validated_evaluate (D:\godot\core\variant\variant_op.h:181)
[1] OperatorEvaluatorDivNZ<__int64,__int64,__int64>::validated_evaluate (D:\godot\core\variant\variant_op.h:181)
[2] GDScriptFunction::call (D:\godot\modules\gdscript\gdscript_vm.cpp:686)
[3] GDScriptInstance::callp (D:\godot\modules\gdscript\gdscript.cpp:1849)
[4] Node::_notification (D:\godot\scene\main\node.cpp:187)
[5] Node2D::_notificationv (D:\godot\scene\2d\node_2d.h:37)
[6] Object::notification (D:\godot\core\object\object.cpp:839)
[7] Node::_propagate_ready (D:\godot\scene\main\node.cpp:232)
[8] Node::_propagate_ready (D:\godot\scene\main\node.cpp:221)
[9] Node::_set_tree (D:\godot\scene\main\node.cpp:2941)
[10] OS_Windows::run (D:\godot\platform\windows\os_windows.cpp:1470)
[11] widechar_main (D:\godot\platform\windows\godot_windows.cpp:182)
[12] _main (D:\godot\platform\windows\godot_windows.cpp:206)
[13] main (D:\godot\platform\windows\godot_windows.cpp:218)
[14] __scrt_common_main_seh (D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:288)
[15] <couldn't map PC to fn name>
-- END OF BACKTRACE --
================================================================

@bitsawer
Copy link
Member

Tested on Windows 10 on current master (a574c02), I get the crash too. Error message is

Stop reason: Exception 0xc0000094 encountered at address 0x7ff7d0032079

0xc0000094 is Windows SEH code for EXCEPTION_INT_DIVIDE_BY_ZERO, which makes sense. Error happens here, because argument right is zero:

*VariantGetInternalPtr<R>::get_ptr(r_ret) = *VariantGetInternalPtr<A>::get_ptr(left) / *VariantGetInternalPtr<B>::get_ptr(right);

Looks like the basic OperatorEvaluatorDivNZ::evaluate() function does have a check for zero division, but the validated_evaluate() and ptr_evaluate() versions don't:

template <class R, class A, class B>
class OperatorEvaluatorDivNZ {
public:
static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) {
const A &a = *VariantGetInternalPtr<A>::get_ptr(&p_left);
const B &b = *VariantGetInternalPtr<B>::get_ptr(&p_right);
if (b == 0) {
r_valid = false;
*r_ret = "Division by zero error";
return;
}
*r_ret = a / b;
r_valid = true;
}
static inline void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) {
*VariantGetInternalPtr<R>::get_ptr(r_ret) = *VariantGetInternalPtr<A>::get_ptr(left) / *VariantGetInternalPtr<B>::get_ptr(right);
}
static void ptr_evaluate(const void *left, const void *right, void *r_ret) {
PtrToArg<R>::encode(PtrToArg<A>::convert(left) / PtrToArg<B>::convert(right), r_ret);
}
static Variant::Type get_return_type() { return GetTypeInfo<R>::VARIANT_TYPE; }
};

@miv391
Copy link
Contributor

miv391 commented Oct 14, 2023

It seems that all OperatorEvaluators in https://github.com/godotengine/godot/blob/a574c0296b38d5f786f249b12e6251e562c528cc/core/variant/variant_op.h suffer from the same problem. If the evaluate() checks some error situations, validated_evaluate() and ptr_evaluate() either crash or return something weird. Some examples:

	var a := -1
	print(12 << a) # prints "0" instead of error message

	var b := 0
	print(12 % b) # crashes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants