From a9806b59a3d74978522b3f823ef9b831a4e66983 Mon Sep 17 00:00:00 2001 From: Naaz nuria Date: Sun, 15 Nov 2026 10:49:56 +0530 Subject: [PATCH] compute img_io line buffer sizes in size_t ppm_in::open, yuv_out::configure and raw_out::configure multiplied the image width by the number of components and/or bytes per sample in 32-bit arithmetic. The width comes from the PGM/PPM header or from the SIZ marker of the codestream being decoded, so a width above 2^31 wraps the product, a few bytes are allocated, and the following fread() or sample loop writes a full line past the end of the buffer. Do the arithmetic in size_t and keep the sizes in size_t members, as pfm_in, ppm_out and tif_out already do. --- src/apps/common/ojph_img_io.h | 9 +++++---- src/apps/others/ojph_img_io.cpp | 12 ++++++------ 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/apps/common/ojph_img_io.h b/src/apps/common/ojph_img_io.h index 85274115..cbf7d026 100644 --- a/src/apps/common/ojph_img_io.h +++ b/src/apps/common/ojph_img_io.h @@ -130,8 +130,9 @@ namespace ojph { mem_fixed_allocator *alloc_p; void *temp_buf; ui32 width, height, num_comps, max_val, max_val_num_bits; - ui32 bytes_per_sample, num_ele_per_line; - ui32 temp_buf_byte_size; + ui32 bytes_per_sample; + size_t num_ele_per_line; + size_t temp_buf_byte_size; ui32 cur_line; si64 start_of_data; @@ -728,7 +729,7 @@ namespace ojph { ui32 bit_depth; ui32 *comp_width; ui8 *buffer; - ui32 buffer_size; + size_t buffer_size; }; //////////////////////////////////////////////////////////////////////////// @@ -767,7 +768,7 @@ namespace ojph { si64 lower_val, upper_val; ui32 width; ui8* buffer; - ui32 buffer_size; + size_t buffer_size; }; //////////////////////////////////////////////////////////////////////////// diff --git a/src/apps/others/ojph_img_io.cpp b/src/apps/others/ojph_img_io.cpp index c39d8a10..0e4fe125 100644 --- a/src/apps/others/ojph_img_io.cpp +++ b/src/apps/others/ojph_img_io.cpp @@ -297,7 +297,7 @@ namespace ojph { close(); OJPH_ERROR(0x03000006, "error in file format for file %s", filename); } - num_ele_per_line = num_comps * width; + num_ele_per_line = num_comps * (size_t)width; bytes_per_sample = max_val > 255 ? 2 : 1; max_val_num_bits = 32u - count_leading_zeros(max_val); bit_depth[2] = bit_depth[1] = bit_depth [0] = max_val_num_bits; @@ -305,11 +305,11 @@ namespace ojph { start_of_data = ojph_ftell(fh); // allocate linebuffer to hold a line of image data - if (temp_buf_byte_size < num_comps * width * bytes_per_sample) + if (temp_buf_byte_size < num_comps * (size_t)width * bytes_per_sample) { if (alloc_p == NULL) { - temp_buf_byte_size = num_comps * width * bytes_per_sample; + temp_buf_byte_size = num_comps * (size_t)width * bytes_per_sample; void* t = temp_buf; if (temp_buf) temp_buf = realloc(temp_buf, temp_buf_byte_size); @@ -323,7 +323,7 @@ namespace ojph { else { assert(temp_buf_byte_size == 0); //cannot reallocate the buffer - temp_buf_byte_size = num_comps * width * bytes_per_sample; + temp_buf_byte_size = num_comps * (size_t)width * bytes_per_sample; alloc_p->pre_alloc_data(temp_buf_byte_size, 0); } } @@ -1477,7 +1477,7 @@ namespace ojph { tw = ojph_max(tw, this->comp_width[i]); } this->width = tw; - buffer_size = tw * (bit_depth > 8 ? 2 : 1); + buffer_size = (size_t)tw * (bit_depth > 8 ? 2 : 1); buffer = (ui8*)malloc(buffer_size); } @@ -1678,7 +1678,7 @@ namespace ojph { } bytes_per_sample = (bit_depth + 7) >> 3; - buffer_size = width * bytes_per_sample; + buffer_size = (size_t)width * bytes_per_sample; buffer = (ui8*)malloc(buffer_size); }