From eea8b61f3f2100ba3ce677ee48f5cb32b107d2ef Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Tue, 19 Dec 2017 08:56:51 +0000 Subject: [PATCH] tiny cleanup make the code for error during jpeg buffer write a little cleaner -- rather than calling the jpeg term method ourselves, have a separate destroy function for the write buffer --- ChangeLog | 5 +++-- libvips/foreign/vips2jpeg.c | 29 +++++++++++++++++++++++------ 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/ChangeLog b/ChangeLog index 09a4be89..3214a921 100644 --- a/ChangeLog +++ b/ChangeLog @@ -2,8 +2,9 @@ - stop window new/free cycling .. faster zoom out on large images in nip2 - fix some compiler warnings - remove the 64-image limit on bandary operations -- better version date, thanks bmwiedemann -- bump wrapper script version, thanks bgilbert +- better version date [bmwiedemann] +- bump wrapper script version [bgilbert] +- fix a memleak with an error during jpeg buffer write [lovell] 15/4/17 started 8.6.0 - supports fits images with leading non-image HDUs, thanks benepo diff --git a/libvips/foreign/vips2jpeg.c b/libvips/foreign/vips2jpeg.c index adae118b..cba26e04 100644 --- a/libvips/foreign/vips2jpeg.c +++ b/libvips/foreign/vips2jpeg.c @@ -84,6 +84,8 @@ * - move exif handling out to exif.c * 27/2/17 * - use dbuf for memory output + * 19/12/17 Lovell + * - fix a leak with an error during buffer output */ /* @@ -618,6 +620,8 @@ write_vips( Write *write, int qfac, const char *profile, if( setjmp( write->eman.jmp ) ) return( -1 ); + /* This should obly be called on a successful write. + */ jpeg_finish_compress( &write->cinfo ); return( 0 ); @@ -725,7 +729,21 @@ init_destination( j_compress_ptr cinfo ) empty_output_buffer( cinfo ); } -/* Cleanup. Copy the set of blocks out as a big lump. +/* Free the buffer writer. + */ +static void +buf_destroy( j_compress_ptr cinfo ) +{ + if( cinfo->dest ) { + OutputBuffer *buf = (OutputBuffer *) cinfo->dest; + + vips_dbuf_destroy( &buf->dbuf ); + } +} + +/* Cleanup. Copy the set of blocks out as a big lump. This is only called by + * libjpeg on successful write --- you must call buf_destroy() explicitly to + * release resources. */ METHODDEF(void) term_destination( j_compress_ptr cinfo ) @@ -798,7 +816,7 @@ vips__jpeg_write_buffer( VipsImage *in, /* Make jpeg compression object. */ if( setjmp( write->eman.jmp ) ) { - /* Here for longjmp() from new_error_exit(). + /* Here for longjmp() from new_error_exit() during setup. */ write_destroy( write ); @@ -810,19 +828,18 @@ vips__jpeg_write_buffer( VipsImage *in, */ buf_dest( &write->cinfo, obuf, olen ); - /* Convert! + /* Convert! Write errors come back here as an error return. */ if( write_vips( write, Q, profile, optimize_coding, progressive, strip, no_subsample, trellis_quant, overshoot_deringing, optimize_scans, quant_table ) ) { - term_destination( &write->cinfo ); - VIPS_FREE( *obuf ); - *olen = 0; + buf_destroy( &write->cinfo ); write_destroy( write ); return( -1 ); } + buf_destroy( &write->cinfo ); write_destroy( write ); return( 0 );