ref: 9dd965b679238e12d72d2ce6b07465dfb5a00a8a
parent: 822061539a0d9a028ee019ead51f75556640c32e
author: Tor Andersson <tor.andersson@artifex.com>
date: Fri Jul 23 12:23:08 EDT 2021
Add special error handling for safe 'protected' functions.
--- a/jsrun.c
+++ b/jsrun.c
@@ -16,7 +16,7 @@
static void js_trystackoverflow(js_State *J)
{
STACK[TOP].type = JS_TLITSTR;
- STACK[TOP].u.litstr = "try stack overflow";
+ STACK[TOP].u.litstr = "exception stack overflow";
++TOP;
js_throw(J);
}
--- a/jsstate.c
+++ b/jsstate.c
@@ -8,6 +8,16 @@
#include <assert.h>
#include <errno.h>
+static int js_ptry(js_State *J) {
+ if (J->trytop == JS_TRYLIMIT) {
+ STACK[TOP].type = JS_TLITSTR;
+ STACK[TOP].u.litstr = "exception stack overflow";
+ ++TOP;
+ return 1;
+ }
+ return 0;
+}
+
static void *js_defaultalloc(void *actx, void *ptr, int size)
{
if (size == 0) {
@@ -31,6 +41,8 @@
int js_ploadstring(js_State *J, const char *filename, const char *source)
{
+ if (js_ptry(J))
+ return 1;
if (js_try(J))
return 1;
js_loadstring(J, filename, source);
@@ -40,6 +52,8 @@
int js_ploadfile(js_State *J, const char *filename)
{
+ if (js_ptry(J))
+ return 1;
if (js_try(J))
return 1;
js_loadfile(J, filename);
@@ -50,6 +64,10 @@
const char *js_trystring(js_State *J, int idx, const char *error)
{
const char *s;
+ if (js_ptry(J)) {
+ js_pop(J, 1);
+ return error;
+ }
if (js_try(J)) {
js_pop(J, 1);
return error;
@@ -62,6 +80,10 @@
double js_trynumber(js_State *J, int idx, double error)
{
double v;
+ if (js_ptry(J)) {
+ js_pop(J, 1);
+ return error;
+ }
if (js_try(J)) {
js_pop(J, 1);
return error;
@@ -74,6 +96,10 @@
int js_tryinteger(js_State *J, int idx, int error)
{
int v;
+ if (js_ptry(J)) {
+ js_pop(J, 1);
+ return error;
+ }
if (js_try(J)) {
js_pop(J, 1);
return error;
@@ -86,6 +112,10 @@
int js_tryboolean(js_State *J, int idx, int error)
{
int v;
+ if (js_ptry(J)) {
+ js_pop(J, 1);
+ return error;
+ }
if (js_try(J)) {
js_pop(J, 1);
return error;
@@ -189,6 +219,11 @@
int js_dostring(js_State *J, const char *source)
{
+ if (js_ptry(J)) {
+ js_report(J, "exception stack overflow");
+ js_pop(J, 1);
+ return 1;
+ }
if (js_try(J)) {
js_report(J, js_trystring(J, -1, "Error"));
js_pop(J, 1);
@@ -204,6 +239,11 @@
int js_dofile(js_State *J, const char *filename)
{
+ if (js_ptry(J)) {
+ js_report(J, "exception stack overflow");
+ js_pop(J, 1);
+ return 1;
+ }
if (js_try(J)) {
js_report(J, js_trystring(J, -1, "Error"));
js_pop(J, 1);