From 1bfb0b5247f4fc8f6677639781ce468543490216 Mon Sep 17 00:00:00 2001 From: DRC Date: Tue, 2 Jun 2020 14:15:37 -0500 Subject: [PATCH] rdppm.c: Fix buf overrun caused by bad binary PPM This extends the fix in 1e81b0c3ea26f4ea8f56de05367469333de64a9f to include binary PPM files with maximum values < 255, thus preventing a malformed binary PPM input file with those specifications from triggering an overrun of the rescale array and potentially crashing cjpeg, TJBench, or any program that uses the tjLoadImage() function. Fixes #433 diff --git a/rdppm.c b/rdppm.c index c0c096218..899436eec 100644 --- a/rdppm.c +++ b/rdppm.c @@ -5,7 +5,7 @@ * Copyright (C) 1991-1997, Thomas G. Lane. * Modified 2009 by Bill Allombert, Guido Vollbeding. * libjpeg-turbo Modifications: - * Copyright (C) 2015, 2016, D. R. Commander. + * Copyright (C) 2015, 2016, 2020, D. R. Commander. * For conditions of distribution and use, see the accompanying README.ijg * file. * @@ -22,6 +22,7 @@ * the file is indeed PPM format). */ +#define JPEG_INTERNALS #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */ #ifdef PPM_SUPPORTED @@ -425,7 +426,7 @@ start_input_ppm (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) /* On 16-bit-int machines we have to be careful of maxval = 65535 */ source->rescale = (JSAMPLE *) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, - (size_t) (((long) maxval + 1L) * + (size_t) (((long) MAX(maxval, 255) + 1L) * sizeof(JSAMPLE))); half_maxval = maxval / 2; for (val = 0; val <= (long) maxval; val++) {