Skip to content

Commit

Permalink
Eliminate unnecessary NULL checks before free()
Browse files Browse the repository at this point in the history
This programming practice (which exists in other code bases as well)
is a by-product of having used early C compilers that did not properly
handle free(NULL).  All modern compilers should properly handle that.

Fixes mozilla#398
  • Loading branch information
dcommander committed Jan 7, 2020
1 parent 166e342 commit fdf8903
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 51 deletions.
36 changes: 16 additions & 20 deletions tjbench.c
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 304,8 @@ static int decomp(unsigned char *srcBuf, unsigned char **jpegBuf,
bailout:
if (file) fclose(file);
if (handle) tjDestroy(handle);
if (dstBuf && dstBufAlloc) free(dstBuf);
if (yuvBuf) free(yuvBuf);
if (dstBufAlloc) free(dstBuf);
free(yuvBuf);
return retval;
}

Expand Down Expand Up @@ -502,18 502,16 @@ static int fullTest(unsigned char *srcBuf, int w, int h, int subsamp,
}

bailout:
if (file) { fclose(file); file = NULL; }
if (file) fclose(file);
if (jpegBuf) {
for (i = 0; i < ntilesw * ntilesh; i ) {
for (i = 0; i < ntilesw * ntilesh; i )
if (jpegBuf[i]) tjFree(jpegBuf[i]);
jpegBuf[i] = NULL;
}
free(jpegBuf); jpegBuf = NULL;
}
if (yuvBuf) { free(yuvBuf); yuvBuf = NULL; }
if (jpegSize) { free(jpegSize); jpegSize = NULL; }
if (tmpBuf) { free(tmpBuf); tmpBuf = NULL; }
if (handle) { tjDestroy(handle); handle = NULL; }
free(jpegBuf);
free(yuvBuf);
free(jpegSize);
free(tmpBuf);
if (handle) tjDestroy(handle);
return retval;
}

Expand Down Expand Up @@ -720,23 718,21 @@ static int decompTest(char *fileName)
jpegBuf[i] = NULL;
}
free(jpegBuf); jpegBuf = NULL;
if (jpegSize) { free(jpegSize); jpegSize = NULL; }
free(jpegSize); jpegSize = NULL;

if (tilew == w && tileh == h) break;
}

bailout:
if (file) { fclose(file); file = NULL; }
if (file) fclose(file);
if (jpegBuf) {
for (i = 0; i < ntilesw * ntilesh; i ) {
for (i = 0; i < ntilesw * ntilesh; i )
if (jpegBuf[i]) tjFree(jpegBuf[i]);
jpegBuf[i] = NULL;
}
free(jpegBuf); jpegBuf = NULL;
}
if (jpegSize) { free(jpegSize); jpegSize = NULL; }
if (srcBuf) { free(srcBuf); srcBuf = NULL; }
if (t) { free(t); t = NULL; }
free(jpegBuf);
free(jpegSize);
free(srcBuf);
free(t);
if (handle) { tjDestroy(handle); handle = NULL; }
return retval;
}
Expand Down
10 changes: 5 additions & 5 deletions tjunittest.c
Original file line number Diff line number Diff line change
Expand Up @@ -410,8 410,8 @@ static void compTest(tjhandle handle, unsigned char **dstBuf,
printf("Done.\n Result in %s\n", tempStr);

bailout:
if (yuvBuf) free(yuvBuf);
if (srcBuf) free(srcBuf);
free(yuvBuf);
free(srcBuf);
}


Expand Down Expand Up @@ -478,8 478,8 @@ static void _decompTest(tjhandle handle, unsigned char *jpegBuf,
printf("\n");

bailout:
if (yuvBuf) free(yuvBuf);
if (dstBuf) free(dstBuf);
free(yuvBuf);
free(dstBuf);
}


Expand Down Expand Up @@ -665,7 665,7 @@ static void bufSizeTest(void)
printf("Done. \n");

bailout:
if (srcBuf) free(srcBuf);
free(srcBuf);
if (dstBuf) tjFree(dstBuf);
if (handle) tjDestroy(handle);
}
Expand Down
6 changes: 3 additions & 3 deletions turbojpeg-jni.c
Original file line number Diff line number Diff line change
Expand Up @@ -1232,9 1232,9 @@ JNIEXPORT jintArray JNICALL Java_org_libjpegturbo_turbojpeg_TJTransformer_transf
free(dstBufs);
}
SAFE_RELEASE(jsrcBuf, jpegBuf);
if (jdstBufs) free(jdstBufs);
if (dstSizes) free(dstSizes);
if (t) free(t);
free(jdstBufs);
free(dstSizes);
free(t);
return jdstSizes;
}

Expand Down
46 changes: 23 additions & 23 deletions turbojpeg.c
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 432,7 @@ DLLEXPORT int tjDestroy(tjhandle handle)

DLLEXPORT void tjFree(unsigned char *buf)
{
if (buf) free(buf);
free(buf);
}


Expand Down Expand Up @@ -462,7 462,7 @@ static tjhandle _tjInitCompress(tjinstance *this)

if (setjmp(this->jerr.setjmp_buffer)) {
/* If we get here, the JPEG code has signaled an error. */
if (this) free(this);
free(this);
return NULL;
}

Expand Down Expand Up @@ -692,7 692,7 @@ DLLEXPORT int tjCompress2(tjhandle handle, const unsigned char *srcBuf,

bailout:
if (cinfo->global_state > CSTATE_START) jpeg_abort_compress(cinfo);
if (row_pointer) free(row_pointer);
free(row_pointer);
if (this->jerr.warning) retval = -1;
this->jerr.stopOnWarning = FALSE;
return retval;
Expand Down Expand Up @@ -866,13 866,13 @@ DLLEXPORT int tjEncodeYUVPlanes(tjhandle handle, const unsigned char *srcBuf,

bailout:
if (cinfo->global_state > CSTATE_START) jpeg_abort_compress(cinfo);
if (row_pointer) free(row_pointer);
free(row_pointer);
for (i = 0; i < MAX_COMPONENTS; i ) {
if (tmpbuf[i] != NULL) free(tmpbuf[i]);
if (_tmpbuf[i] != NULL) free(_tmpbuf[i]);
if (tmpbuf2[i] != NULL) free(tmpbuf2[i]);
if (_tmpbuf2[i] != NULL) free(_tmpbuf2[i]);
if (outbuf[i] != NULL) free(outbuf[i]);
free(tmpbuf[i]);
free(_tmpbuf[i]);
free(tmpbuf2[i]);
free(_tmpbuf2[i]);
free(outbuf[i]);
}
if (this->jerr.warning) retval = -1;
this->jerr.stopOnWarning = FALSE;
Expand Down Expand Up @@ -1062,10 1062,10 @@ DLLEXPORT int tjCompressFromYUVPlanes(tjhandle handle,
bailout:
if (cinfo->global_state > CSTATE_START) jpeg_abort_compress(cinfo);
for (i = 0; i < MAX_COMPONENTS; i ) {
if (tmpbuf[i]) free(tmpbuf[i]);
if (inbuf[i]) free(inbuf[i]);
free(tmpbuf[i]);
free(inbuf[i]);
}
if (_tmpbuf) free(_tmpbuf);
free(_tmpbuf);
if (this->jerr.warning) retval = -1;
this->jerr.stopOnWarning = FALSE;
return retval;
Expand Down Expand Up @@ -1130,7 1130,7 @@ static tjhandle _tjInitDecompress(tjinstance *this)

if (setjmp(this->jerr.setjmp_buffer)) {
/* If we get here, the JPEG code has signaled an error. */
if (this) free(this);
free(this);
return NULL;
}

Expand Down Expand Up @@ -1313,7 1313,7 @@ DLLEXPORT int tjDecompress2(tjhandle handle, const unsigned char *jpegBuf,

bailout:
if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo);
if (row_pointer) free(row_pointer);
free(row_pointer);
if (this->jerr.warning) retval = -1;
this->jerr.stopOnWarning = FALSE;
return retval;
Expand Down Expand Up @@ -1519,11 1519,11 @@ DLLEXPORT int tjDecodeYUVPlanes(tjhandle handle,

bailout:
if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo);
if (row_pointer) free(row_pointer);
free(row_pointer);
for (i = 0; i < MAX_COMPONENTS; i ) {
if (tmpbuf[i] != NULL) free(tmpbuf[i]);
if (_tmpbuf[i] != NULL) free(_tmpbuf[i]);
if (inbuf[i] != NULL) free(inbuf[i]);
free(tmpbuf[i]);
free(_tmpbuf[i]);
free(inbuf[i]);
}
if (this->jerr.warning) retval = -1;
this->jerr.stopOnWarning = FALSE;
Expand Down Expand Up @@ -1731,10 1731,10 @@ DLLEXPORT int tjDecompressToYUVPlanes(tjhandle handle,
bailout:
if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo);
for (i = 0; i < MAX_COMPONENTS; i ) {
if (tmpbuf[i]) free(tmpbuf[i]);
if (outbuf[i]) free(outbuf[i]);
free(tmpbuf[i]);
free(outbuf[i]);
}
if (_tmpbuf) free(_tmpbuf);
free(_tmpbuf);
if (this->jerr.warning) retval = -1;
this->jerr.stopOnWarning = FALSE;
return retval;
Expand Down Expand Up @@ -1979,7 1979,7 @@ DLLEXPORT int tjTransform(tjhandle handle, const unsigned char *jpegBuf,
bailout:
if (cinfo->global_state > CSTATE_START) jpeg_abort_compress(cinfo);
if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo);
if (xinfo) free(xinfo);
free(xinfo);
if (this->jerr.warning) retval = -1;
this->jerr.stopOnWarning = FALSE;
return retval;
Expand Down Expand Up @@ -2074,7 2074,7 @@ DLLEXPORT unsigned char *tjLoadImage(const char *filename, int *width,
bailout:
if (handle) tjDestroy(handle);
if (file) fclose(file);
if (retval < 0 && dstBuf) { free(dstBuf); dstBuf = NULL; }
if (retval < 0) { free(dstBuf); dstBuf = NULL; }
return dstBuf;
}

Expand Down

0 comments on commit fdf8903

Please sign in to comment.