From 4c00e74b4124474a736678e5554f9d8057c78de8 Mon Sep 17 00:00:00 2001 From: Sebastian Rasmussen Date: Sat, 4 Aug 2012 19:31:53 +0200 Subject: Handle exceptions better in mupdfclean pdf_write_document() may throw an exception and this was uncaught up until now. --- apps/mupdfclean.c | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) (limited to 'apps') diff --git a/apps/mupdfclean.c b/apps/mupdfclean.c index 6b185668..fdf9b412 100644 --- a/apps/mupdfclean.c +++ b/apps/mupdfclean.c @@ -161,6 +161,7 @@ int pdfclean_main(int argc, char **argv) int c; int subset; fz_write_options opts; + int write_failed = 0; opts.do_garbage = 0; opts.do_expand = 0; @@ -204,18 +205,29 @@ int pdfclean_main(int argc, char **argv) exit(1); } - xref = pdf_open_document_no_run(ctx, infile); - if (pdf_needs_password(xref)) - if (!pdf_authenticate_password(xref, password)) - fz_throw(ctx, "cannot authenticate password: %s", infile); + fz_try(ctx) + { + xref = pdf_open_document_no_run(ctx, infile); + if (pdf_needs_password(xref)) + if (!pdf_authenticate_password(xref, password)) + fz_throw(ctx, "cannot authenticate password: %s", infile); - /* Only retain the specified subset of the pages */ - if (subset) - retainpages(argc, argv); + /* Only retain the specified subset of the pages */ + if (subset) + retainpages(argc, argv); - pdf_write_document(xref, outfile, &opts); + pdf_write_document(xref, outfile, &opts); + } + fz_always(ctx) + { + pdf_close_document(xref); + } + fz_catch(ctx) + { + write_failed = 1; + } - pdf_close_document(xref); fz_free_context(ctx); - return 0; + + return write_failed ? 1 : 0; } -- cgit v1.2.3 From ffb37aaa386095d61846419c860eb46b587b6b1d Mon Sep 17 00:00:00 2001 From: Sebastian Rasmussen Date: Sat, 4 Aug 2012 19:35:39 +0200 Subject: Make use of fz_always instead of repeating code for error and normal path --- apps/mudraw.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'apps') diff --git a/apps/mudraw.c b/apps/mudraw.c index dcd7dafd..0f11a7a1 100644 --- a/apps/mudraw.c +++ b/apps/mudraw.c @@ -130,15 +130,17 @@ static void drawpage(fz_context *ctx, fz_document *doc, int pagenum) dev = fz_new_list_device(ctx, list); fz_run_page(doc, page, dev, fz_identity, &cookie); } - fz_catch(ctx) + fz_always(ctx) { fz_free_device(dev); + dev = NULL; + } + fz_catch(ctx) + { fz_free_display_list(ctx, list); fz_free_page(doc, page); fz_throw(ctx, "cannot draw page %d in file '%s'", pagenum, filename); } - fz_free_device(dev); - dev = NULL; } if (showxml) -- cgit v1.2.3 From 0db354a6cffc42d1d0ade2b434504b66ec8efba4 Mon Sep 17 00:00:00 2001 From: Sebastian Rasmussen Date: Mon, 6 Aug 2012 01:09:05 +0200 Subject: Add option to mudraw to process further files upon error --- apps/mudraw.c | 71 ++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 43 insertions(+), 28 deletions(-) (limited to 'apps') diff --git a/apps/mudraw.c b/apps/mudraw.c index 0f11a7a1..07b9e1c3 100644 --- a/apps/mudraw.c +++ b/apps/mudraw.c @@ -31,6 +31,7 @@ static int width = 0; static int height = 0; static int fit = 0; static int errored = 0; +static int ignore_errors = 0; static fz_text_sheet *sheet = NULL; static fz_colorspace *colorspace; @@ -68,6 +69,7 @@ static void usage(void) "\t-G gamma\tgamma correct output\n" "\t-I\tinvert output\n" "\t-l\tprint outline\n" + "\t-i\tignore errors and continue with the next file\n" "\tpages\tcomma separated list of ranges\n"); exit(1); } @@ -447,7 +449,7 @@ int main(int argc, char **argv) fz_var(doc); - while ((c = fz_getopt(argc, argv, "lo:p:r:R:ab:dgmtx5G:Iw:h:f")) != -1) + while ((c = fz_getopt(argc, argv, "lo:p:r:R:ab:dgmtx5G:Iw:h:fi")) != -1) { switch (c) { @@ -469,6 +471,7 @@ int main(int argc, char **argv) case 'h': height = atof(fz_optarg); break; case 'f': fit = 1; break; case 'I': invert++; break; + case 'i': ignore_errors = 1; break; default: usage(); break; } } @@ -531,41 +534,53 @@ int main(int argc, char **argv) { while (fz_optind < argc) { - filename = argv[fz_optind++]; - files++; - fz_try(ctx) { - doc = fz_open_document(ctx, filename); - } - fz_catch(ctx) - { - fz_throw(ctx, "cannot open document: %s", filename); - } + filename = argv[fz_optind++]; + files++; - if (fz_needs_password(doc)) - if (!fz_authenticate_password(doc, password)) - fz_throw(ctx, "cannot authenticate password: %s", filename); + fz_try(ctx) + { + doc = fz_open_document(ctx, filename); + } + fz_catch(ctx) + { + fz_throw(ctx, "cannot open document: %s", filename); + } - if (showxml || showtext == TEXT_XML) - printf("\n", filename); + if (fz_needs_password(doc)) + if (!fz_authenticate_password(doc, password)) + fz_throw(ctx, "cannot authenticate password: %s", filename); - if (showoutline) - drawoutline(ctx, doc); + if (showxml || showtext == TEXT_XML) + printf("\n", filename); - if (showtext || showxml || showtime || showmd5 || output) - { - if (fz_optind == argc || !isrange(argv[fz_optind])) - drawrange(ctx, doc, "1-"); - if (fz_optind < argc && isrange(argv[fz_optind])) - drawrange(ctx, doc, argv[fz_optind++]); - } + if (showoutline) + drawoutline(ctx, doc); + + if (showtext || showxml || showtime || showmd5 || output) + { + if (fz_optind == argc || !isrange(argv[fz_optind])) + drawrange(ctx, doc, "1-"); + if (fz_optind < argc && isrange(argv[fz_optind])) + drawrange(ctx, doc, argv[fz_optind++]); + } + + if (showxml || showtext == TEXT_XML) + printf("\n"); - if (showxml || showtext == TEXT_XML) - printf("\n"); + fz_close_document(doc); + doc = NULL; + } + fz_catch(ctx) + { + if (!ignore_errors) + fz_rethrow(ctx); - fz_close_document(doc); - doc = NULL; + fz_close_document(doc); + doc = NULL; + fz_warn(ctx, "ignoring error in '%s'", filename); + } } } fz_catch(ctx) -- cgit v1.2.3 From 9c021377135a931a11bab58d6fe30353a98f1092 Mon Sep 17 00:00:00 2001 From: Sebastian Rasmussen Date: Mon, 6 Aug 2012 15:07:52 +0200 Subject: Check for a display list before trying to render it in pdfapp Previously fix 13943b92f10796efb175e769afe5b0aea85d879a introduced continued rendering of further pages for documents where one page failed to load. However, if the entire page tree was missing from a PDF document then no display list would be obtained, yet MuPDF tried to render the display list causing a null pointer dereference. Now, check for a valid display list before trying to render it. --- apps/pdfapp.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) (limited to 'apps') diff --git a/apps/pdfapp.c b/apps/pdfapp.c index 50a89610..1a17e172 100644 --- a/apps/pdfapp.c +++ b/apps/pdfapp.c @@ -326,9 +326,12 @@ static void pdfapp_showpage(pdfapp_t *app, int loadpage, int drawpage, int repai /* Extract text */ app->page_sheet = fz_new_text_sheet(app->ctx); app->page_text = fz_new_text_page(app->ctx, app->page_bbox); - tdev = fz_new_text_device(app->ctx, app->page_sheet, app->page_text); - fz_run_display_list(app->page_list, tdev, fz_identity, fz_infinite_bbox, &cookie); - fz_free_device(tdev); + if (app->page_list) + { + tdev = fz_new_text_device(app->ctx, app->page_sheet, app->page_text); + fz_run_display_list(app->page_list, tdev, fz_identity, fz_infinite_bbox, &cookie); + fz_free_device(tdev); + } } if (drawpage) @@ -365,9 +368,12 @@ static void pdfapp_showpage(pdfapp_t *app, int loadpage, int drawpage, int repai #endif app->image = fz_new_pixmap_with_bbox(app->ctx, colorspace, bbox); fz_clear_pixmap_with_value(app->ctx, app->image, 255); - idev = fz_new_draw_device(app->ctx, app->image); - fz_run_display_list(app->page_list, idev, ctm, bbox, &cookie); - fz_free_device(idev); + if (app->page_list) + { + idev = fz_new_draw_device(app->ctx, app->image); + fz_run_display_list(app->page_list, idev, ctm, bbox, &cookie); + fz_free_device(idev); + } if (app->invert) fz_invert_pixmap(app->ctx, app->image); } -- cgit v1.2.3