1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-05 12:10:55 +00:00

Prevent infinite recursion during printing

This commit is contained in:
Mikael Hermansson
2025-07-22 09:59:34 +02:00
parent 71a9948157
commit 836a1a0b02
4 changed files with 70 additions and 14 deletions

View File

@@ -43,6 +43,19 @@
#endif
static ErrorHandlerList *error_handler_list = nullptr;
static thread_local bool is_printing_error = false;
static void _err_print_fallback(const char *p_function, const char *p_file, int p_line, const char *p_error_details, ErrorHandlerType p_type, bool p_reentrance) {
if (p_reentrance) {
fprintf(stderr, "While attempting to print an error, another error was printed:\n");
}
fprintf(stderr, "%s: %s\n", _error_handler_type_string(p_type), p_error_details);
if (p_function && p_file) {
fprintf(stderr, " at: %s (%s:%i)\n", p_function, p_file, p_line);
}
}
void add_error_handler(ErrorHandlerList *p_handler) {
// If p_handler is already in error_handler_list
@@ -91,12 +104,21 @@ void _err_print_error(const char *p_function, const char *p_file, int p_line, co
// Main error printing function.
void _err_print_error(const char *p_function, const char *p_file, int p_line, const char *p_error, const char *p_message, bool p_editor_notify, ErrorHandlerType p_type) {
if (is_printing_error) {
// Fallback if we're already printing an error, to prevent infinite recursion.
const char *err_details = (p_message && *p_message) ? p_message : p_error;
_err_print_fallback(p_function, p_file, p_line, err_details, p_type, true);
return;
}
is_printing_error = true;
if (OS::get_singleton()) {
OS::get_singleton()->print_error(p_function, p_file, p_line, p_error, p_message, p_editor_notify, (Logger::ErrorType)p_type, ScriptServer::capture_script_backtraces(false));
} else {
// Fallback if errors happen before OS init or after it's destroyed.
const char *err_details = (p_message && *p_message) ? p_message : p_error;
fprintf(stderr, "%s: %s\n at: %s (%s:%i)\n", _error_handler_type_string(p_type), err_details, p_function, p_file, p_line);
_err_print_fallback(p_function, p_file, p_line, err_details, p_type, false);
}
_global_lock();
@@ -108,6 +130,8 @@ void _err_print_error(const char *p_function, const char *p_file, int p_line, co
}
_global_unlock();
is_printing_error = false;
}
// For printing errors when we may crash at any point, so we must flush ASAP a lot of lines
@@ -116,11 +140,19 @@ void _err_print_error(const char *p_function, const char *p_file, int p_line, co
void _err_print_error_asap(const String &p_error, ErrorHandlerType p_type) {
const char *err_details = p_error.utf8().get_data();
if (is_printing_error) {
// Fallback if we're already printing an error, to prevent infinite recursion.
_err_print_fallback(nullptr, nullptr, 0, err_details, p_type, true);
return;
}
is_printing_error = true;
if (OS::get_singleton()) {
OS::get_singleton()->printerr("%s: %s\n", _error_handler_type_string(p_type), err_details);
} else {
// Fallback if errors happen before OS init or after it's destroyed.
fprintf(stderr, "%s: %s\n", _error_handler_type_string(p_type), err_details);
_err_print_fallback(nullptr, nullptr, 0, err_details, p_type, false);
}
_global_lock();
@@ -132,6 +164,8 @@ void _err_print_error_asap(const String &p_error, ErrorHandlerType p_type) {
}
_global_unlock();
is_printing_error = false;
}
// Errors with message. (All combinations of p_error and p_message as String or char*.)