Skip to content

Commit

Permalink
fix: avoid unchecked nullptr deref on invalid lcms transform
Browse files Browse the repository at this point in the history
On invalid data the requested transform from lcms may return nun
successfully. Hence we need to check the return value and early
the function to avoid a segfault.
To distinguish the concrete error, a new error code (45) has been
introduced.

Signed-off-by: Levente Polyak <levente@leventepolyak.net>
  • Loading branch information
anthraxx committed Aug 7, 2021
1 parent a6ff122 commit 68a1bb0
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 0 deletions.
1 change: 1 addition & 0 deletions rust/ffi.rs
Expand Up @@ -31,6 +31,7 @@ pub enum pngquant_error {
LIBPNG_FATAL_ERROR = 25,
WRONG_INPUT_COLOR_TYPE = 26,
LIBPNG_INIT_ERROR = 35,
LCMS_FATAL_ERROR = 45,
TOO_LARGE_FILE = 98,
TOO_LOW_QUALITY = 99,
}
Expand Down
6 changes: 6 additions & 0 deletions rwpng.c
Expand Up @@ -376,6 +376,12 @@ static pngquant_error rwpng_read_image24_libpng(FILE *infile, png24_image *mainp
hOutProfile, TYPE_RGBA_8,
INTENT_PERCEPTUAL,
omp_get_max_threads() > 1 ? cmsFLAGS_NOCACHE : 0);
if(!hTransform) {
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
cmsCloseProfile(hOutProfile);
cmsCloseProfile(hInProfile);
return LCMS_FATAL_ERROR;
}

#pragma omp parallel for \
if (mainprog_ptr->height*mainprog_ptr->width > 8000) \
Expand Down
1 change: 1 addition & 0 deletions rwpng.h
Expand Up @@ -32,6 +32,7 @@ typedef enum {
LIBPNG_FATAL_ERROR = 25,
WRONG_INPUT_COLOR_TYPE = 26,
LIBPNG_INIT_ERROR = 35,
LCMS_FATAL_ERROR = 45,
TOO_LARGE_FILE = 98,
TOO_LOW_QUALITY = 99,
} pngquant_error;
Expand Down

0 comments on commit 68a1bb0

Please sign in to comment.