Skip to content

Commit

Permalink
Fix JPEG error handling (Issue #415)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelrsweet committed Apr 1, 2021
1 parent ba61a3e commit 369b2ea
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changes in HTMLDOC v1.9.12

- Fixed a crash bug with "data:" URIs and EPUB output (Issue #410)
- Fixed JPEG error handling (Issue #415)
- Fixed crash bugs with bogus table attributes (Issue #416, Issue #417)
- Fixed a crash bug with malformed URIs (Issue #418)
- Fixed a crash bug with malformed GIF files (Issue #423)
Expand Down
9 changes: 8 additions & 1 deletion htmldoc/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -1010,8 +1010,15 @@ file_rlookup(const char *filename) /* I - Filename */


for (i = web_files, wc = web_cache; i > 0; i --, wc ++)
{
if (!strcmp(wc->name, filename))
return (wc->url);
{
if (!strncmp(wc->url, "data:", 5))
return ("data URL");
else
return (wc->url);
}
}

return (filename);
}
Expand Down
38 changes: 31 additions & 7 deletions htmldoc/image.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -1336,6 +1336,15 @@ image_load_gif(image_t *img, /* I - Image pointer */
}


typedef struct hd_jpeg_err_s // JPEG error manager extension
{
struct jpeg_error_mgr jerr; // JPEG error manager information
jmp_buf retbuf; // setjmp() return buffer
char message[JMSG_LENGTH_MAX];
// Last error message
} hd_jpeg_err_t;


/*
* 'image_load_jpeg()' - Load a JPEG image file.
*/
Expand All @@ -1347,14 +1356,21 @@ image_load_jpeg(image_t *img, /* I - Image pointer */
int load_data)/* I - 1 = load image data, 0 = just info */
{
struct jpeg_decompress_struct cinfo; /* Decompressor info */
struct jpeg_error_mgr jerr; /* Error handler info */
JSAMPROW row; /* Sample row pointer */
hd_jpeg_err_t jerr; // JPEG error handler
JSAMPROW row; /* Sample row pointer */


jpeg_std_error(&jerr);
jerr.error_exit = jpeg_error_handler;
jpeg_std_error(&jerr.jerr);
jerr.jerr.error_exit = jpeg_error_handler;

cinfo.err = &jerr;
if (setjmp(jerr.retbuf))
{
progress_error(HD_ERROR_BAD_FORMAT, "%s (%s)", jerr.message, file_rlookup(img->filename));
jpeg_destroy_decompress(&cinfo);
return (-1);
}

cinfo.err = (struct jpeg_error_mgr *)&jerr;
jpeg_create_decompress(&cinfo);
jpeg_stdio_src(&cinfo, fp);
jpeg_read_header(&cinfo, (boolean)1);
Expand Down Expand Up @@ -1797,9 +1813,17 @@ image_unload(image_t *img) // I - Image
*/

static void
jpeg_error_handler(j_common_ptr)
jpeg_error_handler(j_common_ptr p) // Common JPEG data
{
return;
hd_jpeg_err_t *jerr = (hd_jpeg_err_t *)p->err;
// JPEG error handler


// Save the error message in the string buffer...
(jerr->jerr.format_message)(p, jerr->message);

// Return to the point we called setjmp()...
longjmp(jerr->retbuf, 1);
}


Expand Down
5 changes: 5 additions & 0 deletions htmldoc/ps-pdf.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -1404,6 +1404,8 @@ pspdf_prepare_page(int page) /* I - Page number */


DEBUG_printf(("pspdf_prepare_page(%d)\n", page));
if (page < 0 || page >= num_pages)
return;

/*
* Make a page number; use roman numerals for the table of contents
Expand Down Expand Up @@ -12285,6 +12287,9 @@ write_trailer(FILE *out, /* I - Output file */

for (j = 1; j <= TocDocCount; j ++)
{
if (chapter_starts[j] < 0)
continue;

page = pages + chapter_starts[j];
start = chapter_starts[j] - chapter_starts[1] + 1;
type = 'D';
Expand Down

0 comments on commit 369b2ea

Please sign in to comment.