Eliminate unnecessary NULL checks before free()
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 #398
diff --git a/tjbench.c b/tjbench.c
index 13a5bde..228e7ef 100644
--- a/tjbench.c
+++ b/tjbench.c
@@ -304,8 +304,8 @@
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;
}
@@ -502,18 +502,16 @@
}
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;
}
@@ -720,23 +718,21 @@
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;
}
diff --git a/tjunittest.c b/tjunittest.c
index da7c6ff..546e5c7 100644
--- a/tjunittest.c
+++ b/tjunittest.c
@@ -410,8 +410,8 @@
printf("Done.\n Result in %s\n", tempStr);
bailout:
- if (yuvBuf) free(yuvBuf);
- if (srcBuf) free(srcBuf);
+ free(yuvBuf);
+ free(srcBuf);
}
@@ -478,8 +478,8 @@
printf("\n");
bailout:
- if (yuvBuf) free(yuvBuf);
- if (dstBuf) free(dstBuf);
+ free(yuvBuf);
+ free(dstBuf);
}
@@ -665,7 +665,7 @@
printf("Done. \n");
bailout:
- if (srcBuf) free(srcBuf);
+ free(srcBuf);
if (dstBuf) tjFree(dstBuf);
if (handle) tjDestroy(handle);
}
diff --git a/turbojpeg-jni.c b/turbojpeg-jni.c
index 9992479..9363450 100644
--- a/turbojpeg-jni.c
+++ b/turbojpeg-jni.c
@@ -1232,9 +1232,9 @@
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;
}
diff --git a/turbojpeg.c b/turbojpeg.c
index 7f607d1..6226bc9 100644
--- a/turbojpeg.c
+++ b/turbojpeg.c
@@ -432,7 +432,7 @@
DLLEXPORT void tjFree(unsigned char *buf)
{
- if (buf) free(buf);
+ free(buf);
}
@@ -462,7 +462,7 @@
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;
}
@@ -692,7 +692,7 @@
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;
@@ -866,13 +866,13 @@
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;
@@ -1062,10 +1062,10 @@
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;
@@ -1130,7 +1130,7 @@
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;
}
@@ -1313,7 +1313,7 @@
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;
@@ -1519,11 +1519,11 @@
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;
@@ -1731,10 +1731,10 @@
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;
@@ -1979,7 +1979,7 @@
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;
@@ -2074,7 +2074,7 @@
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;
}