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 <smcv@collabora.com>
  • Loading branch information
smcv authored and slouken committed Mar 11, 2024
1 parent f98f0bc commit 5baa3c8
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/IMG_jpg.c
Expand Up @@ -482,6 +482,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 @@ -490,6 +491,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, SDL_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 5baa3c8

Please sign in to comment.