diff --git a/src/decode.c b/src/decode.c index a29c6a46e..dcd11eb5d 100644 --- a/src/decode.c +++ b/src/decode.c @@ -199,7 +199,7 @@ _setimage(ImagingDecoderObject *decoder, PyObject *args) { state->bytes = (state->bits * state->xsize + 7) / 8; } /* malloc check ok, overflow checked above */ - state->buffer = (UINT8 *)malloc(state->bytes); + state->buffer = (UINT8 *)calloc(1, state->bytes); if (!state->buffer) { return ImagingError_MemoryError(); } diff --git a/src/encode.c b/src/encode.c index f92ba62c2..c47255952 100644 --- a/src/encode.c +++ b/src/encode.c @@ -264,7 +264,7 @@ _setimage(ImagingEncoderObject *encoder, PyObject *args) { } state->bytes = (state->bits * state->xsize + 7) / 8; /* malloc check ok, overflow checked above */ - state->buffer = (UINT8 *)malloc(state->bytes); + state->buffer = (UINT8 *)calloc(1, state->bytes); if (!state->buffer) { return ImagingError_MemoryError(); } diff --git a/src/libImaging/Jpeg2KDecode.c b/src/libImaging/Jpeg2KDecode.c index f086848e9..6af0f2eae 100644 --- a/src/libImaging/Jpeg2KDecode.c +++ b/src/libImaging/Jpeg2KDecode.c @@ -861,6 +861,10 @@ j2k_decode_entry(Imaging im, ImagingCodecState state) { state->state = J2K_STATE_FAILED; goto quick_exit; } + /* Undefined behavior, sometimes decode_tile_data doesn't + fill the buffer and we do things with it later, leading + to valgrind errors. */ + memset(new, 0, tile_info.data_size); state->buffer = new; buffer_size = tile_info.data_size; }