From b178d1621a989325fb90547111e30acc5fb9f044 Mon Sep 17 00:00:00 2001 From: Yay295 Date: Fri, 26 Aug 2022 21:20:07 -0500 Subject: [PATCH] prevent Valgrind errors about using uninitialized memory --- src/libImaging/Storage.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/libImaging/Storage.c b/src/libImaging/Storage.c index aa02750a1..e8b6ec213 100644 --- a/src/libImaging/Storage.c +++ b/src/libImaging/Storage.c @@ -381,7 +381,7 @@ ImagingDestroyArray(Imaging im) { Imaging ImagingAllocateArray(Imaging im, int dirty, int block_size) { - int y, line_in_block, current_block; + int y, x, line_in_block, current_block; ImagingMemoryArena arena = &ImagingDefaultArena; ImagingMemoryBlock block = {NULL, 0}; int aligned_linesize, lines_per_block, blocks_count; @@ -442,6 +442,18 @@ ImagingAllocateArray(Imaging im, int dirty, int block_size) { im->destroy = ImagingDestroyArray; + /* Zero-out the unused bytes for image modes that + only use 3 of 4 bytes to prevent Valgrind errors */ + if (dirty && im->bands == 3 && im->pixelsize == 4) { + for (y = 0; y < im->ysize; y++) { + char *line = im->image[y] + 3; + for (x = 0; x < im->xsize; x++) { + *line = 0; + line += 4; + } + } + } + return im; }