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

Show better description to users when std::bad_alloc happens #13783

Merged
merged 1 commit into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/client/clientlauncher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,9 +277,8 @@ bool ClientLauncher::run(GameStartData &start_data, const Settings &cmd_args)

#ifdef NDEBUG
catch (std::exception &e) {
std::string error_message = "Some exception: \"";
error_message += e.what();
error_message += "\"";
error_message = "Some exception: ";
error_message.append(debug_describe_exc(e));
errorstream << error_message << std::endl;
}
#endif
Expand Down
9 changes: 9 additions & 0 deletions src/debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,

#ifdef _MSC_VER
#include <dbghelp.h>
#include <windows.h>
#include <eh.h>
#include "version.h"
#include "filesys.h"
#endif
Expand Down Expand Up @@ -74,6 +76,13 @@ void fatal_error_fn(const char *msg, const char *file,
abort();
}

std::string debug_describe_exc(const std::exception &e)
{
if (dynamic_cast<const std::bad_alloc*>(&e))
return "C++ out of memory";
grorp marked this conversation as resolved.
Show resolved Hide resolved
return std::string("\"").append(e.what()).append("\"");
}

#ifdef _MSC_VER

const char *Win32ExceptionCodeToString(DWORD exception_code)
Expand Down
12 changes: 5 additions & 7 deletions src/debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "gettime.h"
#include "log.h"

#ifdef _WIN32
#include <windows.h>
#ifdef _MSC_VER
#include <eh.h>
#endif
#ifdef _MSC_VER
#define FUNCTION_NAME __FUNCTION__
#else
#define FUNCTION_NAME __PRETTY_FUNCTION__
Expand Down Expand Up @@ -75,6 +71,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,

#define sanity_check(expr) SANITY_CHECK(expr)

std::string debug_describe_exc(const std::exception &e);

void debug_set_exception_handler();

Expand All @@ -86,9 +83,10 @@ void debug_set_exception_handler();
#define BEGIN_DEBUG_EXCEPTION_HANDLER try {
#define END_DEBUG_EXCEPTION_HANDLER \
} catch (std::exception &e) { \
std::string e_descr = debug_describe_exc(e); \
errorstream << "An unhandled exception occurred: " \
<< e.what() << std::endl; \
FATAL_ERROR(e.what()); \
<< e_descr << std::endl; \
FATAL_ERROR(e_descr.c_str()); \
}
#else
// Dummy ones
Expand Down
3 changes: 2 additions & 1 deletion src/script/common/c_internal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ int script_exception_wrapper(lua_State *L, lua_CFunction f)
} catch (const char *s) { // Catch and convert exceptions.
lua_pushstring(L, s);
} catch (std::exception &e) {
lua_pushstring(L, e.what());
std::string e_descr = debug_describe_exc(e);
lua_pushlstring(L, e_descr.c_str(), e_descr.size());
}
return lua_error(L); // Rethrow as a Lua error.
}
Expand Down