From 8c69132579967861972b1a3634da97961464ea8d Mon Sep 17 00:00:00 2001 From: Alexander Date: Tue, 23 May 2017 17:26:13 +0300 Subject: [PATCH] fix another memory leak --- decode.c | 1 + libImaging/Imaging.h | 1 + libImaging/ZipDecode.c | 20 ++++++++++++++++++++ 3 files changed, 22 insertions(+) diff --git a/decode.c b/decode.c index d7fe02fae..f749a40a7 100644 --- a/decode.c +++ b/decode.c @@ -779,6 +779,7 @@ PyImaging_ZipDecoderNew(PyObject* self, PyObject* args) return NULL; decoder->decode = ImagingZipDecode; + decoder->cleanup = ImagingZipDecodeCleanup; ((ZIPSTATE*)decoder->state.context)->interlaced = interlaced; diff --git a/libImaging/Imaging.h b/libImaging/Imaging.h index 2030821f2..99fff7f67 100644 --- a/libImaging/Imaging.h +++ b/libImaging/Imaging.h @@ -455,6 +455,7 @@ extern int ImagingXbmEncode(Imaging im, ImagingCodecState state, #ifdef HAVE_LIBZ extern int ImagingZipDecode(Imaging im, ImagingCodecState state, UINT8* buffer, int bytes); +extern int ImagingZipDecodeCleanup(ImagingCodecState state); extern int ImagingZipEncode(Imaging im, ImagingCodecState state, UINT8* buffer, int bytes); extern int ImagingZipEncodeCleanup(ImagingCodecState state); diff --git a/libImaging/ZipDecode.c b/libImaging/ZipDecode.c index 2f6cdce34..dc5a7684e 100644 --- a/libImaging/ZipDecode.c +++ b/libImaging/ZipDecode.c @@ -86,6 +86,7 @@ ImagingZipDecode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes) if (err < 0) { state->errcode = IMAGING_CODEC_CONFIG; free(context->previous); + context->previous = NULL; return -1; } @@ -127,6 +128,7 @@ ImagingZipDecode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes) else state->errcode = IMAGING_CODEC_CONFIG; free(context->previous); + context->previous = NULL; inflateEnd(&context->z_stream); return -1; } @@ -192,6 +194,7 @@ ImagingZipDecode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes) default: state->errcode = IMAGING_CODEC_UNKNOWN; free(context->previous); + context->previous = NULL; inflateEnd(&context->z_stream); return -1; } @@ -259,6 +262,7 @@ ImagingZipDecode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes) state->errcode = IMAGING_CODEC_BROKEN; */ free(context->previous); + context->previous = NULL; inflateEnd(&context->z_stream); return -1; /* end of file (errcode=0) */ @@ -275,4 +279,20 @@ ImagingZipDecode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes) } + +int ImagingZipDecodeCleanup(ImagingCodecState state){ + /* called to fee the decompression engine when the decode terminates + due to a corrupt or truncated image + */ + ZIPSTATE* context = (ZIPSTATE*) state->context; + + /* Clean up */ + if (context->previous) { + inflateEnd(&context->z_stream); + free(context->previous); + context->previous = NULL; + } + return -1; +} + #endif