Commit be9d78c6 authored by Deucе's avatar Deucе 👌🏾

Eliminate crash in js.on_exit()

Have js.on_exit() throw an exciption with a useful(?) error when the
scope already has private data, and that data is not an on_exit list.

This fixes #182.
parent 1d1e8671
Pipeline #685 passed with stage
in 16 minutes and 47 seconds
......@@ -59,6 +59,11 @@ enum {
,PROP_GLOBAL
};
struct onexit_private {
char signature[7];
str_list_t list;
};
static JSBool js_get(JSContext *cx, JSObject *obj, jsid id, jsval *vp)
{
jsval idval;
......@@ -601,8 +606,9 @@ js_on_exit(JSContext *cx, uintN argc, jsval *arglist)
JSObject *glob=JS_GetGlobalObject(cx);
jsval *argv=JS_ARGV(cx, arglist);
global_private_t* pd;
str_list_t list;
struct onexit_private *oep = NULL;
str_list_t oldlist;
str_list_t list;
char *p = NULL;
JS_SET_RVAL(cx, arglist, JSVAL_VOID);
......@@ -615,11 +621,30 @@ js_on_exit(JSContext *cx, uintN argc, jsval *arglist)
list=pd->exit_func;
}
else {
list=(str_list_t)JS_GetPrivate(cx,scope);
if(list==NULL) {
list=strListInit();
JS_SetPrivate(cx,scope,list);
oep=(struct onexit_private *)JS_GetPrivate(cx,scope);
if(oep==NULL) {
oep = malloc(sizeof(*oep));
if (oep == NULL) {
JS_ReportError(cx, "Unable to allocate memory for on_exit() list");
return JS_FALSE;
}
memcpy(oep->signature, "onexit", 7);
oep->list=strListInit();
JS_SetPrivate(cx,scope,oep);
}
else {
if (oep->signature[0] != 'o'
|| oep->signature[0] != 'n'
|| oep->signature[0] != 'e'
|| oep->signature[0] != 'x'
|| oep->signature[0] != 'i'
|| oep->signature[0] != 't'
|| oep->signature[0] != 0) {
JS_ReportError(cx, "on_exit not available in %s scopes", JS_GetClass(cx, scope)->name);
return JS_FALSE;
}
}
list = oep->list;
}
JSVALUE_TO_MSTRING(cx, argv[0], p, NULL);
......@@ -633,7 +658,7 @@ js_on_exit(JSContext *cx, uintN argc, jsval *arglist)
if(glob==scope)
pd->exit_func=list;
else
JS_SetPrivate(cx,scope,list);
oep->list = list;
}
return(JS_TRUE);
}
......@@ -780,8 +805,15 @@ void DLLCALL js_EvalOnExit(JSContext *cx, JSObject *obj, js_callback_t* cb)
pt=(global_private_t *)JS_GetPrivate(cx,JS_GetGlobalObject(cx));
list=pt->exit_func;
}
else
list=JS_GetPrivate(cx,obj);
else {
struct onexit_private *oep = JS_GetPrivate(cx,obj);
if (oep == NULL)
list = NULL;
else {
list = oep->list;
free(oep);
}
}
cb->auto_terminate=FALSE;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment