Skip to content

Commit

Permalink
JPEG: Add error-recovery when saving with libjpeg
Browse files Browse the repository at this point in the history
Because we have set up libjpeg to use my_error_exit, we need to call
setjmp() before the first time it might possibly call longjmp().
Otherwise, on error we'll do a non-local goto to an uninitialized
pointer and crash.

Resolves: #429
Signed-off-by: Simon McVittie <[email protected]>
  • Loading branch information
smcv committed Mar 11, 2024
1 parent e5b3ef1 commit b0c271c
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/IMG_jpg.c
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,7 @@ struct savejpeg_vars
{
struct jpeg_compress_struct cinfo;
struct my_error_mgr jerr;
Sint64 original_offset;
};

static int JPEG_SaveJPEG_RW(struct savejpeg_vars *vars, SDL_Surface *jpeg_surface, SDL_RWops *dst, int quality)
Expand All @@ -489,6 +490,14 @@ static int JPEG_SaveJPEG_RW(struct savejpeg_vars *vars, SDL_Surface *jpeg_surfac
vars->cinfo.err = lib.jpeg_std_error(&vars->jerr.errmgr);
vars->jerr.errmgr.error_exit = my_error_exit;
vars->jerr.errmgr.output_message = output_no_message;
vars->original_offset = SDL_RWtell(dst);

if(setjmp(vars->jerr.escape)) {
/* If we get here, libjpeg found an error */
lib.jpeg_destroy_compress(&vars->cinfo);
SDL_RWseek(dst, vars->original_offset, RW_SEEK_SET);
return IMG_SetError("Error saving JPEG with libjpeg");
}

lib.jpeg_create_compress(&vars->cinfo);
jpeg_SDL_RW_dest(&vars->cinfo, dst);
Expand Down

0 comments on commit b0c271c

Please sign in to comment.