Skip to content

Commit 3451b6c

Browse files
committed
Guard state initialization with try to avoid panic in initialization.
1 parent 88f6d86 commit 3451b6c

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

jsstate.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,12 +322,18 @@ js_State *js_newstate(js_Alloc alloc, void *actx, int flags)
322322
J->nextref = 0;
323323
J->gcthresh = 0; /* reaches stability within ~ 2-5 GC cycles */
324324

325+
if (js_try(J)) {
326+
js_freestate(J);
327+
return NULL;
328+
}
329+
325330
J->R = jsV_newobject(J, JS_COBJECT, NULL);
326331
J->G = jsV_newobject(J, JS_COBJECT, NULL);
327332
J->E = jsR_newenvironment(J, J->G, NULL);
328333
J->GE = J->E;
329334

330335
jsB_init(J);
331336

337+
js_endtry(J);
332338
return J;
333339
}

main.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,10 @@ main(int argc, char **argv)
311311
}
312312

313313
J = js_newstate(NULL, NULL, strict ? JS_STRICT : 0);
314+
if (!J) {
315+
fprintf(stderr, "Could not initialize MuJS.\n");
316+
exit(1);
317+
}
314318

315319
js_newcfunction(J, jsB_gc, "gc", 0);
316320
js_setglobal(J, "gc");

0 commit comments

Comments
 (0)