Don't store Exception objects' messages on the stack.

This commit is contained in:
Wayward Heart 2024-08-27 13:55:25 -05:00
parent 347ef9f53c
commit cf43cf005b
4 changed files with 15 additions and 23 deletions

View file

@ -70,16 +70,14 @@ const RbException excToRbExc[] = {
MKXP /* MKXPError */ MKXP /* MKXPError */
}; };
VALUE excToRbClass(const Exception &exc) { void raiseRbExc(Exception *exc) {
VALUE str = rb_str_new2(exc->msg.c_str());
RbData *data = getRbData(); RbData *data = getRbData();
return data->exc[excToRbExc[exc.type]]; VALUE excClass = data->exc[excToRbExc[exc->type]];
}
void raiseRbExc(Exception exc) { delete exc;
RbData *data = getRbData();
VALUE excClass = data->exc[excToRbExc[exc.type]];
rb_raise(excClass, "%s", exc.msg); rb_exc_raise(rb_class_new_instance(1, &str, excClass));
} }
void raiseDisposedAccess(VALUE self) { void raiseDisposedAccess(VALUE self) {
@ -311,9 +309,7 @@ int rb_get_args(int argc, VALUE *argv, const char *format, ...) {
/* Raising here is probably fine, right? /* Raising here is probably fine, right?
* If any methods allocate something with a destructor before * If any methods allocate something with a destructor before
* calling this then they can probably be fixed to not do that. */ * calling this then they can probably be fixed to not do that. */
Exception e(*exc); raiseRbExc(exc);
delete exc;
rb_raise(excToRbClass(e), "%s", e.msg);
} }
return 0; return 0;
@ -333,7 +329,7 @@ static void *gvl_guard(void *args) {
try{ try{
return gvl_args->func(gvl_args->args); return gvl_args->func(gvl_args->args);
} catch (const Exception &e) { } catch (const Exception &e) {
gvl_args->exc = new Exception(e.type, e.msg); gvl_args->exc = new Exception(e);
} }
return 0; return 0;
} }
@ -346,7 +342,7 @@ void *drop_gvl_guard(void *(*func)(void *), void *args,
Exception *&exc = gvl_args.exc; Exception *&exc = gvl_args.exc;
if (exc){ if (exc){
Exception e(exc->type, exc->msg); Exception e(*exc);
delete exc; delete exc;
throw e; throw e;
} }

View file

@ -75,8 +75,7 @@ RbData *getRbData();
struct Exception; struct Exception;
VALUE excToRbClass(const Exception &exc); void raiseRbExc(Exception *exc);
void raiseRbExc(Exception exc);
#if RAPI_MAJOR >= 2 #if RAPI_MAJOR >= 2
void *drop_gvl_guard(void *(*func)(void *), void *args, void *drop_gvl_guard(void *(*func)(void *), void *args,
@ -494,9 +493,7 @@ static inline VALUE rb_file_open_str(VALUE filename, const char *mode) {
exc = new Exception(e); \ exc = new Exception(e); \
} \ } \
if (exc) { \ if (exc) { \
Exception e(*exc); \ raiseRbExc(exc); \
delete exc; \
rb_raise(excToRbClass(e), "%s", e.msg); \
} \ } \
return Qnil; \ return Qnil; \
} }

View file

@ -44,9 +44,7 @@ serializableDump(int, VALUE *, VALUE self)
} }
if (exc) { if (exc) {
Exception e(exc->type, exc->msg); raiseRbExc(exc);
delete exc;
rb_raise(excToRbClass(e), "%s", e.msg);
} }
return data; return data;

View file

@ -48,7 +48,7 @@ struct Exception
}; };
Type type; Type type;
char msg[512]; std::string msg;
Exception(Type type, const char *format, ...) Exception(Type type, const char *format, ...)
: type(type) : type(type)
@ -56,7 +56,8 @@ struct Exception
va_list ap; va_list ap;
va_start(ap, format); va_start(ap, format);
vsnprintf(msg, sizeof(msg), format, ap); msg.resize(512);
vsnprintf(&msg[0], msg.size(), format, ap);
va_end(ap); va_end(ap);
} }