diff options
-rw-r--r-- | fitz/base_error.c | 24 | ||||
-rw-r--r-- | fitz/fitz.h | 7 | ||||
-rw-r--r-- | pdf/pdf_parse.c | 12 |
3 files changed, 15 insertions, 28 deletions
diff --git a/fitz/base_error.c b/fitz/base_error.c index 606e9c2a..ce048a1e 100644 --- a/fitz/base_error.c +++ b/fitz/base_error.c @@ -43,12 +43,6 @@ void fz_warn(fz_context *ctx, char *fmt, ...) /* Error context */ -int fz_too_deeply_nested(fz_context *ctx) -{ - fz_error_context *ex = ctx->error; - return ex->top + 1 >= nelem(ex->stack); -} - static void throw(fz_error_context *ex) { if (ex->top >= 0) { @@ -60,15 +54,21 @@ static void throw(fz_error_context *ex) } } -void fz_push_try(fz_error_context *ex) +int fz_push_try(fz_error_context *ex) { assert(ex); - if (ex->top + 1 >= nelem(ex->stack)) - { - fprintf(stderr, "exception stack overflow!\n"); - exit(EXIT_FAILURE); - } ex->top++; + /* Normal case, get out of here quick */ + if (ex->top < nelem(ex->stack)-1) + return 1; + /* We reserve the top slot on the exception stack purely to cope with + * the case when we overflow. If we DO hit this, then we 'throw' + * immediately - returning 0 stops the setjmp happening and takes us + * direct to the always/catch clauses. */ + assert(ex->top == nelem(ex->stack)-1); + strcpy(ex->message, "exception stack overflow!\n"); + ex->stack[ex->top].code = 1; + return 0; } char *fz_caught(fz_context *ctx) diff --git a/fitz/fitz.h b/fitz/fitz.h index 63278cb1..dd9424a9 100644 --- a/fitz/fitz.h +++ b/fitz/fitz.h @@ -237,8 +237,8 @@ void fz_var_imp(void *); */ #define fz_try(ctx) \ - if (fz_push_try(ctx->error), \ - (ctx->error->stack[ctx->error->top].code = fz_setjmp(ctx->error->stack[ctx->error->top].buffer)) == 0) \ + if (fz_push_try(ctx->error) && \ + ((ctx->error->stack[ctx->error->top].code = fz_setjmp(ctx->error->stack[ctx->error->top].buffer)) == 0))\ { do { #define fz_always(ctx) \ @@ -251,11 +251,10 @@ void fz_var_imp(void *); } \ if (ctx->error->stack[ctx->error->top--].code) -void fz_push_try(fz_error_context *ex); +int fz_push_try(fz_error_context *ex); void fz_throw(fz_context *, char *, ...) __printflike(2, 3); void fz_rethrow(fz_context *); void fz_warn(fz_context *ctx, char *fmt, ...) __printflike(2, 3); -int fz_too_deeply_nested(fz_context *ctx); /* fz_flush_warnings: Flush any repeated warnings. diff --git a/pdf/pdf_parse.c b/pdf/pdf_parse.c index 6f27099f..dd710891 100644 --- a/pdf/pdf_parse.c +++ b/pdf/pdf_parse.c @@ -244,9 +244,6 @@ pdf_parse_array(pdf_document *xref, fz_stream *file, pdf_lexbuf *buf) break; case PDF_TOK_OPEN_ARRAY: - if (fz_too_deeply_nested(ctx)) - fz_throw(ctx, "nested too deep, not parsing array"); - obj = pdf_parse_array(xref, file, buf); pdf_array_push(ary, obj); pdf_drop_obj(obj); @@ -254,9 +251,6 @@ pdf_parse_array(pdf_document *xref, fz_stream *file, pdf_lexbuf *buf) break; case PDF_TOK_OPEN_DICT: - if (fz_too_deeply_nested(ctx)) - fz_throw(ctx, "nested too deep, not parsing dict"); - obj = pdf_parse_dict(xref, file, buf); pdf_array_push(ary, obj); pdf_drop_obj(obj); @@ -354,16 +348,10 @@ pdf_parse_dict(pdf_document *xref, fz_stream *file, pdf_lexbuf *buf) switch (tok) { case PDF_TOK_OPEN_ARRAY: - if (fz_too_deeply_nested(ctx)) - fz_throw(ctx, "nested too deep, not parsing array"); - val = pdf_parse_array(xref, file, buf); break; case PDF_TOK_OPEN_DICT: - if (fz_too_deeply_nested(ctx)) - fz_throw(ctx, "nested too deep, not parsing array"); - val = pdf_parse_dict(xref, file, buf); break; |