From 7407371deb8b87e3049c9f1e8b04eac04a1fe6cc Mon Sep 17 00:00:00 2001 From: Josh Ware Date: Wed, 27 Aug 2014 19:13:42 +1000 Subject: [PATCH] NULLing pointers on cleanup --- PIL/ImageFile.py | 8 ++++---- libImaging/Jpeg2KDecode.c | 38 ++++++++++++++++++++------------------ libImaging/Jpeg2KEncode.c | 10 ++++++---- 3 files changed, 30 insertions(+), 26 deletions(-) diff --git a/PIL/ImageFile.py b/PIL/ImageFile.py index 81549d8af..1b5c2ebb2 100644 --- a/PIL/ImageFile.py +++ b/PIL/ImageFile.py @@ -135,8 +135,8 @@ class ImageFile(Image.Image): self.map = None use_mmap = self.filename and len(self.tile) == 1 # As of pypy 2.1.0, memory mapping was failing here. - use_mmap = use_mmap and not hasattr(sys, 'pypy_version_info') - + use_mmap = use_mmap and not hasattr(sys, 'pypy_version_info') + readonly = 0 # look for read/seek overrides @@ -473,7 +473,7 @@ def _save(im, fp, tile, bufsize=0): break if s < 0: raise IOError("encoder error %d when writing image file" % s) - #e.cleanup() + e.cleanup() else: # slight speedup: compress to real file object for e, b, o, a in tile: @@ -484,7 +484,7 @@ def _save(im, fp, tile, bufsize=0): s = e.encode_to_file(fh, bufsize) if s < 0: raise IOError("encoder error %d when writing image file" % s) - #e.cleanup() + e.cleanup() try: fp.flush() except: pass diff --git a/libImaging/Jpeg2KDecode.c b/libImaging/Jpeg2KDecode.c index 1477e68b9..bd6b59a62 100644 --- a/libImaging/Jpeg2KDecode.c +++ b/libImaging/Jpeg2KDecode.c @@ -68,7 +68,7 @@ j2k_skip(OPJ_OFF_T p_nb_bytes, void *p_user_data) typedef void (*j2k_unpacker_t)(opj_image_t *in, const JPEG2KTILEINFO *tileInfo, - const UINT8 *data, + const UINT8 *data, Imaging im); struct j2k_decode_unpacker { @@ -335,7 +335,7 @@ j2ku_srgb_rgb(opj_image_t *in, const JPEG2KTILEINFO *tileinfo, UINT8 *row = (UINT8 *)im->image[y0 + y] + x0 * 4; for (n = 0; n < 3; ++n) data[n] = &cdata[n][csiz[n] * y * w]; - + for (x = 0; x < w; ++x) { for (n = 0; n < 3; ++n) { UINT32 word = 0; @@ -388,7 +388,7 @@ j2ku_sycc_rgb(opj_image_t *in, const JPEG2KTILEINFO *tileinfo, UINT8 *row_start = row; for (n = 0; n < 3; ++n) data[n] = &cdata[n][csiz[n] * y * w]; - + for (x = 0; x < w; ++x) { for (n = 0; n < 3; ++n) { UINT32 word = 0; @@ -442,7 +442,7 @@ j2ku_srgba_rgba(opj_image_t *in, const JPEG2KTILEINFO *tileinfo, UINT8 *row = (UINT8 *)im->image[y0 + y] + x0 * 4; for (n = 0; n < 4; ++n) data[n] = &cdata[n][csiz[n] * y * w]; - + for (x = 0; x < w; ++x) { for (n = 0; n < 4; ++n) { UINT32 word = 0; @@ -494,7 +494,7 @@ j2ku_sycca_rgba(opj_image_t *in, const JPEG2KTILEINFO *tileinfo, UINT8 *row_start = row; for (n = 0; n < 4; ++n) data[n] = &cdata[n][csiz[n] * y * w]; - + for (x = 0; x < w; ++x) { for (n = 0; n < 4; ++n) { UINT32 word = 0; @@ -587,13 +587,13 @@ j2k_decode_entry(Imaging im, ImagingCodecState state, /* Setup decompression context */ context->error_msg = NULL; - + opj_set_default_decoder_parameters(¶ms); params.cp_reduce = context->reduce; params.cp_layer = context->layers; - + codec = opj_create_decompress(context->format); - + if (!codec) { state->errcode = IMAGING_CODEC_BROKEN; state->state = J2K_STATE_FAILED; @@ -616,7 +616,7 @@ j2k_decode_entry(Imaging im, ImagingCodecState state, state->state = J2K_STATE_FAILED; goto quick_exit; } - + for (n = 1; n < image->numcomps; ++n) { if (image->comps[n].dx != 1 || image->comps[n].dy != 1) { state->errcode = IMAGING_CODEC_BROKEN; @@ -624,8 +624,8 @@ j2k_decode_entry(Imaging im, ImagingCodecState state, goto quick_exit; } } - - /* + + /* Colorspace Number of components PIL mode ------------------------------------------------------ sRGB 3 RGB @@ -633,22 +633,22 @@ j2k_decode_entry(Imaging im, ImagingCodecState state, gray 1 L or I gray 2 LA YCC 3 YCbCr - - + + If colorspace is unspecified, we assume: - + Number of components Colorspace ----------------------------------------- 1 gray 2 gray (+ alpha) 3 sRGB 4 sRGB (+ alpha) - + */ - + /* Find the correct unpacker */ color_space = image->color_space; - + if (color_space == OPJ_CLRSPC_UNSPECIFIED) { switch (image->numcomps) { case 1: case 2: color_space = OPJ_CLRSPC_GRAY; break; @@ -668,7 +668,7 @@ j2k_decode_entry(Imaging im, ImagingCodecState state, if (!unpack) { state->errcode = IMAGING_CODEC_BROKEN; state->state = J2K_STATE_FAILED; - goto quick_exit; + goto quick_exit; } /* Decode the image tile-by-tile; this means we only need use as much @@ -797,6 +797,8 @@ ImagingJpeg2KDecodeCleanup(ImagingCodecState state) { if (context->error_msg) free ((void *)context->error_msg); + context->error_msg = NULL; + if (context->decoder) ImagingIncrementalCodecDestroy(context->decoder); diff --git a/libImaging/Jpeg2KEncode.c b/libImaging/Jpeg2KEncode.c index b4c5284cb..5d6caf30c 100644 --- a/libImaging/Jpeg2KEncode.c +++ b/libImaging/Jpeg2KEncode.c @@ -303,7 +303,7 @@ j2k_encode_entry(Imaging im, ImagingCodecState state, prec = 16; bpp = 12; } else if (strcmp (im->mode, "LA") == 0) { - components = 2; + components = 2; color_space = OPJ_CLRSPC_GRAY; pack = j2k_pack_la; } else if (strcmp (im->mode, "RGB") == 0) { @@ -340,7 +340,7 @@ j2k_encode_entry(Imaging im, ImagingCodecState state, context->error_msg = NULL; opj_set_default_encoder_parameters(¶ms); - + params.image_offset_x0 = context->offset_x; params.image_offset_y0 = context->offset_y; @@ -546,8 +546,8 @@ ImagingJpeg2KEncode(Imaging im, ImagingCodecState state, UINT8 *buf, int bytes) return -1; if (state->state == J2K_STATE_START) { - int seekable = (context->format != OPJ_CODEC_J2K - ? INCREMENTAL_CODEC_SEEKABLE + int seekable = (context->format != OPJ_CODEC_J2K + ? INCREMENTAL_CODEC_SEEKABLE : INCREMENTAL_CODEC_NOT_SEEKABLE); context->encoder = ImagingIncrementalCodecCreate(j2k_encode_entry, @@ -582,6 +582,8 @@ ImagingJpeg2KEncodeCleanup(ImagingCodecState state) { if (context->error_msg) free ((void *)context->error_msg); + context->error_msg = NULL; + if (context->encoder) ImagingIncrementalCodecDestroy(context->encoder);