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
This commit is contained in:
36
tjbench.c
36
tjbench.c
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
10
tjunittest.c
10
tjunittest.c
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -1232,9 +1232,9 @@ bailout:
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
46
turbojpeg.c
46
turbojpeg.c
@@ -432,7 +432,7 @@ DLLEXPORT int tjDestroy(tjhandle handle)
|
||||
|
||||
DLLEXPORT void tjFree(unsigned char *buf)
|
||||
{
|
||||
if (buf) free(buf);
|
||||
free(buf);
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
@@ -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;
|
||||
@@ -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;
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
@@ -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;
|
||||
@@ -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;
|
||||
@@ -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;
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user