diff --git a/turbojpeg-mp.c b/turbojpeg-mp.c index 5699869d..f58995e7 100644 --- a/turbojpeg-mp.c +++ b/turbojpeg-mp.c @@ -125,10 +125,10 @@ DLLEXPORT int GET_NAME(tj3Compress, BITS_IN_JSAMPLE) jpeg_finish_compress(cinfo); bailout: - if (cinfo->global_state > CSTATE_START) { - if (alloc) (*cinfo->dest->term_destination) (cinfo); + if (cinfo->global_state > CSTATE_START && alloc) + (*cinfo->dest->term_destination) (cinfo); + if (cinfo->global_state > CSTATE_START || retval == -1) jpeg_abort_compress(cinfo); - } free(row_pointer); if (this->jerr.warning) retval = -1; return retval; diff --git a/turbojpeg.c b/turbojpeg.c index a764608f..81bd6691 100644 --- a/turbojpeg.c +++ b/turbojpeg.c @@ -1589,10 +1589,10 @@ DLLEXPORT int tj3CompressFromYUVPlanes8(tjhandle handle, jpeg_finish_compress(cinfo); bailout: - if (cinfo->global_state > CSTATE_START) { - if (alloc) (*cinfo->dest->term_destination) (cinfo); + if (cinfo->global_state > CSTATE_START && alloc) + (*cinfo->dest->term_destination) (cinfo); + if (cinfo->global_state > CSTATE_START || retval == -1) jpeg_abort_compress(cinfo); - } for (i = 0; i < MAX_COMPONENTS; i++) { free(tmpbuf[i]); free(inbuf[i]);