From e64a57b1e9c2ba6c6f54eff73c414be9e78dafbe Mon Sep 17 00:00:00 2001 From: Robin Watts Date: Thu, 8 Dec 2011 16:26:31 +0000 Subject: Fix exception handling in pdfdraw to avoid leaks. Remove use of 'die', in place of proper exception handling. --- apps/pdfdraw.c | 86 ++++++++++++++++++++++++++++++---------------------------- 1 file changed, 45 insertions(+), 41 deletions(-) (limited to 'apps') diff --git a/apps/pdfdraw.c b/apps/pdfdraw.c index c1d2a779..6d43dc01 100644 --- a/apps/pdfdraw.c +++ b/apps/pdfdraw.c @@ -36,12 +36,6 @@ struct { int minpage, maxpage; } timing; -static void die(fz_error error) -{ - fz_error_handle(error, "aborting"); - exit(1); -} - static void usage(void) { fprintf(stderr, @@ -111,7 +105,7 @@ static void drawpage(pdf_xref *xref, int pagenum) } fz_catch(ctx) { - die(fz_error_note(1, "cannot load page %d in file '%s'", pagenum, filename)); + fz_throw(ctx, "cannot load page %d in file '%s'", pagenum, filename); } list = NULL; @@ -126,7 +120,8 @@ static void drawpage(pdf_xref *xref, int pagenum) } fz_catch(ctx) { - die(fz_error_note(1, "cannot draw page %d in file '%s'", pagenum, filename)); + fz_free_device(dev); + fz_throw(ctx, "cannot draw page %d in file '%s'", pagenum, filename); } fz_free_device(dev); } @@ -325,11 +320,12 @@ int main(int argc, char **argv) char *password = ""; int grayscale = 0; int accelerate = 1; - pdf_xref *xref; - fz_error error; + pdf_xref *xref = NULL; int c; fz_context *ctx; + fz_var(xref); + while ((c = fz_getopt(argc, argv, "lo:p:r:R:Aab:dgmtx5G:I")) != -1) { switch (c) @@ -397,45 +393,53 @@ int main(int argc, char **argv) if (showxml) printf("\n"); - while (fz_optind < argc) + fz_try(ctx) { - filename = argv[fz_optind++]; - - fz_try(ctx) + while (fz_optind < argc) { - xref = pdf_open_xref(ctx, filename, password); - } - fz_catch(ctx) - { - die(fz_error_note(error, "cannot open document: %s", filename)); - } + filename = argv[fz_optind++]; - fz_try(ctx) - { - pdf_load_page_tree(xref); - } - fz_catch(ctx) - { - die(fz_error_note(error, "cannot load page tree: %s", filename)); - } + fz_try(ctx) + { + xref = pdf_open_xref(ctx, filename, password); + } + fz_catch(ctx) + { + fz_throw(ctx, "cannot open document: %s", filename); + } - if (showxml) - printf("\n", filename); + fz_try(ctx) + { + pdf_load_page_tree(xref); + } + fz_catch(ctx) + { + fz_throw(ctx, "cannot load page tree: %s", filename); + } - if (showoutline) - drawoutline(xref); + if (showxml) + printf("\n", filename); - if (showtext || showxml || showtime || showmd5 || output) - { - if (fz_optind == argc || !isrange(argv[fz_optind])) - drawrange(xref, "1-"); - if (fz_optind < argc && isrange(argv[fz_optind])) - drawrange(xref, argv[fz_optind++]); - } + if (showoutline) + drawoutline(xref); + + if (showtext || showxml || showtime || showmd5 || output) + { + if (fz_optind == argc || !isrange(argv[fz_optind])) + drawrange(xref, "1-"); + if (fz_optind < argc && isrange(argv[fz_optind])) + drawrange(xref, argv[fz_optind++]); + } - if (showxml) - printf("\n"); + if (showxml) + printf("\n"); + pdf_free_xref(xref); + xref = NULL; + } + } + fz_catch(ctx) + { pdf_free_xref(xref); } -- cgit v1.2.3