From 3451b6ca9632b2146eb30164eae764921d8025b6 Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Thu, 9 Jun 2022 14:58:02 +0200 Subject: [PATCH] Guard state initialization with try to avoid panic in initialization. --- jsstate.c | 6 ++++++ main.c | 4 ++++ 2 files changed, 10 insertions(+) diff --git a/jsstate.c b/jsstate.c index 7ac4463..922c169 100644 --- a/jsstate.c +++ b/jsstate.c @@ -322,6 +322,11 @@ js_State *js_newstate(js_Alloc alloc, void *actx, int flags) J->nextref = 0; J->gcthresh = 0; /* reaches stability within ~ 2-5 GC cycles */ + if (js_try(J)) { + js_freestate(J); + return NULL; + } + J->R = jsV_newobject(J, JS_COBJECT, NULL); J->G = jsV_newobject(J, JS_COBJECT, NULL); J->E = jsR_newenvironment(J, J->G, NULL); @@ -329,5 +334,6 @@ js_State *js_newstate(js_Alloc alloc, void *actx, int flags) jsB_init(J); + js_endtry(J); return J; } diff --git a/main.c b/main.c index c0b2077..fb8c986 100644 --- a/main.c +++ b/main.c @@ -311,6 +311,10 @@ main(int argc, char **argv) } J = js_newstate(NULL, NULL, strict ? JS_STRICT : 0); + if (!J) { + fprintf(stderr, "Could not initialize MuJS.\n"); + exit(1); + } js_newcfunction(J, jsB_gc, "gc", 0); js_setglobal(J, "gc");