From 692bb2407e9bb1c8d8276e1d359d0b8f9b13df11 Mon Sep 17 00:00:00 2001 From: Eric Soroos Date: Mon, 2 Jun 2025 11:36:53 +0200 Subject: [PATCH] Revert "Fix memory leak in TiffEncode" This reverts commit e2e40c54568236d2504921eb0b335cdab734a7d5. --- src/encode.c | 2 -- src/libImaging/TiffDecode.c | 42 ++++++++++++++++++------------------- src/libImaging/TiffDecode.h | 2 -- 3 files changed, 21 insertions(+), 25 deletions(-) diff --git a/src/encode.c b/src/encode.c index e56494036..7c365a74f 100644 --- a/src/encode.c +++ b/src/encode.c @@ -703,8 +703,6 @@ PyImaging_LibTiffEncoderNew(PyObject *self, PyObject *args) { return NULL; } - encoder->cleanup = ImagingLibTiffEncodeCleanup; - num_core_tags = sizeof(core_tags) / sizeof(int); for (pos = 0; pos < tags_size; pos++) { item = PyList_GetItemRef(tags, pos); diff --git a/src/libImaging/TiffDecode.c b/src/libImaging/TiffDecode.c index 173eca160..9a2db95b4 100644 --- a/src/libImaging/TiffDecode.c +++ b/src/libImaging/TiffDecode.c @@ -929,27 +929,6 @@ ImagingLibTiffSetField(ImagingCodecState state, ttag_t tag, ...) { return status; } -int -ImagingLibTiffEncodeCleanup(ImagingCodecState state) { - TIFFSTATE *clientstate = (TIFFSTATE *)state->context; - TIFF *tiff = clientstate->tiff; - - if (!tiff) { - return 0; - } - // TIFFClose in libtiff calls tif_closeproc and TIFFCleanup - if (clientstate->fp) { - // Python will manage the closing of the file rather than libtiff - // So only call TIFFCleanup - TIFFCleanup(tiff); - } else { - // When tif_closeproc refers to our custom _tiffCloseProc though, - // that is fine, as it does not close the file - TIFFClose(tiff); - } - return 0; -} - int ImagingLibTiffEncode(Imaging im, ImagingCodecState state, UINT8 *buffer, int bytes) { /* One shot encoder. Encode everything to the tiff in the clientstate. @@ -1031,6 +1010,16 @@ ImagingLibTiffEncode(Imaging im, ImagingCodecState state, UINT8 *buffer, int byt TRACE(("Encode Error, row %d\n", state->y)); state->errcode = IMAGING_CODEC_BROKEN; + // TIFFClose in libtiff calls tif_closeproc and TIFFCleanup + if (clientstate->fp) { + // Python will manage the closing of the file rather than libtiff + // So only call TIFFCleanup + TIFFCleanup(tiff); + } else { + // When tif_closeproc refers to our custom _tiffCloseProc though, + // that is fine, as it does not close the file + TIFFClose(tiff); + } if (!clientstate->fp) { free(clientstate->data); } @@ -1047,11 +1036,22 @@ ImagingLibTiffEncode(Imaging im, ImagingCodecState state, UINT8 *buffer, int byt TRACE(("Error flushing the tiff")); // likely reason is memory. state->errcode = IMAGING_CODEC_MEMORY; + if (clientstate->fp) { + TIFFCleanup(tiff); + } else { + TIFFClose(tiff); + } if (!clientstate->fp) { free(clientstate->data); } return -1; } + TRACE(("Closing \n")); + if (clientstate->fp) { + TIFFCleanup(tiff); + } else { + TIFFClose(tiff); + } // reset the clientstate metadata to use it to read out the buffer. clientstate->loc = 0; clientstate->size = clientstate->eof; // redundant? diff --git a/src/libImaging/TiffDecode.h b/src/libImaging/TiffDecode.h index 77808b543..22361210d 100644 --- a/src/libImaging/TiffDecode.h +++ b/src/libImaging/TiffDecode.h @@ -40,8 +40,6 @@ ImagingLibTiffInit(ImagingCodecState state, int fp, uint32_t offset); extern int ImagingLibTiffEncodeInit(ImagingCodecState state, char *filename, int fp); extern int -ImagingLibTiffEncodeCleanup(ImagingCodecState state); -extern int ImagingLibTiffMergeFieldInfo( ImagingCodecState state, TIFFDataType field_type, int key, int is_var_length );