summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobin Watts <robin.watts@artifex.com>2012-08-07 18:08:41 +0100
committerRobin Watts <robin.watts@artifex.com>2012-08-07 19:09:33 +0100
commit7c871225996e814fe5ce513097a8eefca0b9294c (patch)
tree8baf4542affb6eab48f9f04b2fece2f368e97b84
parentd13391b011532794563f0e2c3911ee72eece7c39 (diff)
downloadmupdf-7c871225996e814fe5ce513097a8eefca0b9294c.tar.xz
Remove fz_too_deeply_nested.
Rather than specifically checking for the nesting getting too deep and hence throwing an error, the error handling should do this for itself. Make it so that we spot the depth being too great in the fz_try() and throw to the fz_catch().
-rw-r--r--fitz/base_error.c24
-rw-r--r--fitz/fitz.h7
-rw-r--r--pdf/pdf_parse.c12
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;