From 29c655c0a6affc0359e499162e8308663eb4d04f Mon Sep 17 00:00:00 2001 From: Markus Elfring Date: Wed, 11 Mar 2015 17:41:01 +0100 Subject: Bug #149: Deletion of unnecessary checks before calls of the function "free" The function "free" is documented in the way that no action shall occur for a passed null pointer. It is therefore not needed that a function caller repeats a corresponding check. http://stackoverflow.com/questions/18775608/free-a-null-pointer-anyway-or-check-first This issue was fixed by using the software Coccinelle 1.0.0-rc24. Signed-off-by: Markus Elfring --- tests/libtest/lib539.c | 3 +-- tests/libtest/lib568.c | 3 +-- tests/libtest/lib569.c | 4 +--- tests/libtest/lib570.c | 4 +--- tests/libtest/lib571.c | 4 +--- tests/libtest/lib572.c | 3 +-- tests/libtest/lib582.c | 6 ++---- tests/server/getpart.c | 9 +++------ tests/server/rtspd.c | 44 ++++++++++++++------------------------------ tests/server/sws.c | 44 ++++++++++++++------------------------------ tests/server/tftpd.c | 6 ++---- tests/unit/unit1305.c | 4 +--- 12 files changed, 42 insertions(+), 92 deletions(-) (limited to 'tests') diff --git a/tests/libtest/lib539.c b/tests/libtest/lib539.c index 923893fb2..053d29806 100644 --- a/tests/libtest/lib539.c +++ b/tests/libtest/lib539.c @@ -83,8 +83,7 @@ int test(char *URL) test_cleanup: curl_slist_free_all(slist); - if(newURL) - free(newURL); + free(newURL); curl_easy_cleanup(curl); curl_global_cleanup(); diff --git a/tests/libtest/lib568.c b/tests/libtest/lib568.c index 12fc5e5d9..b6f5ab7ca 100644 --- a/tests/libtest/lib568.c +++ b/tests/libtest/lib568.c @@ -159,8 +159,7 @@ test_cleanup: if(sdpf) fclose(sdpf); - if(stream_uri) - free(stream_uri); + free(stream_uri); if(custom_headers) curl_slist_free_all(custom_headers); diff --git a/tests/libtest/lib569.c b/tests/libtest/lib569.c index 222f1e024..bc4887276 100644 --- a/tests/libtest/lib569.c +++ b/tests/libtest/lib569.c @@ -115,9 +115,7 @@ test_cleanup: if(idfile) fclose(idfile); - if(stream_uri) - free(stream_uri); - + free(stream_uri); curl_easy_cleanup(curl); curl_global_cleanup(); diff --git a/tests/libtest/lib570.c b/tests/libtest/lib570.c index 93ccab8ea..178f58f96 100644 --- a/tests/libtest/lib570.c +++ b/tests/libtest/lib570.c @@ -102,9 +102,7 @@ int test(char *URL) } test_cleanup: - - if(stream_uri) - free(stream_uri); + free(stream_uri); curl_easy_cleanup(curl); curl_global_cleanup(); diff --git a/tests/libtest/lib571.c b/tests/libtest/lib571.c index 3270300de..d8dad396b 100644 --- a/tests/libtest/lib571.c +++ b/tests/libtest/lib571.c @@ -195,9 +195,7 @@ int test(char *URL) } test_cleanup: - - if(stream_uri) - free(stream_uri); + free(stream_uri); if(protofile) fclose(protofile); diff --git a/tests/libtest/lib572.c b/tests/libtest/lib572.c index 9b23c01a2..fd547b8b3 100644 --- a/tests/libtest/lib572.c +++ b/tests/libtest/lib572.c @@ -165,8 +165,7 @@ test_cleanup: if(paramsf) fclose(paramsf); - if(stream_uri) - free(stream_uri); + free(stream_uri); if(custom_headers) curl_slist_free_all(custom_headers); diff --git a/tests/libtest/lib582.c b/tests/libtest/lib582.c index 952efb4ed..4c3ea6f60 100644 --- a/tests/libtest/lib582.c +++ b/tests/libtest/lib582.c @@ -357,10 +357,8 @@ test_cleanup: fclose(hd_src); /* free local memory */ - if(sockets.read.sockets) - free(sockets.read.sockets); - if(sockets.write.sockets) - free(sockets.write.sockets); + free(sockets.read.sockets); + free(sockets.write.sockets); return res; } diff --git a/tests/server/getpart.c b/tests/server/getpart.c index fb86596e7..f773dd058 100644 --- a/tests/server/getpart.c +++ b/tests/server/getpart.c @@ -209,8 +209,7 @@ static int decodedata(char **buf, /* dest buffer */ ** let's just assume it is an OOM condition, currently we have ** no input for this function that decodes to zero length data. */ - if(buf64) - free(buf64); + free(buf64); return GPE_OUT_OF_MEMORY; } @@ -435,15 +434,13 @@ int getpart(char **outbuf, size_t *outlen, } /* while */ - if(buffer) - free(buffer); + free(buffer); if(error != GPE_OK) { if(error == GPE_END_OF_FILE) error = GPE_OK; else { - if(*outbuf) - free(*outbuf); + free(*outbuf); *outbuf = NULL; *outlen = 0; } diff --git a/tests/server/rtspd.c b/tests/server/rtspd.c index 72422ecf5..0293f59cf 100644 --- a/tests/server/rtspd.c +++ b/tests/server/rtspd.c @@ -523,8 +523,7 @@ static int ProcessRequest(struct httprequest *req) } while(ptr && *ptr); logmsg("Done parsing server commands"); } - if(cmd) - free(cmd); + free(cmd); } } else { @@ -993,8 +992,7 @@ static int send_doc(curl_socket_t sock, struct httprequest *req) } if(got_exit_signal) { - if(ptr) - free(ptr); + free(ptr); return -1; } @@ -1005,8 +1003,7 @@ static int send_doc(curl_socket_t sock, struct httprequest *req) logmsg("fopen() failed with error: %d %s", error, strerror(error)); logmsg("Error opening file: %s", filename); logmsg("Couldn't open test file"); - if(ptr) - free(ptr); + free(ptr); return 0; } else { @@ -1015,18 +1012,15 @@ static int send_doc(curl_socket_t sock, struct httprequest *req) fclose(stream); if(error) { logmsg("getpart() failed with error: %d", error); - if(ptr) - free(ptr); + free(ptr); return 0; } } } if(got_exit_signal) { - if(ptr) - free(ptr); - if(cmd) - free(cmd); + free(ptr); + free(cmd); return -1; } @@ -1050,10 +1044,8 @@ static int send_doc(curl_socket_t sock, struct httprequest *req) logmsg("fopen() failed with error: %d %s", error, strerror(error)); logmsg("Error opening file: %s", RESPONSE_DUMP); logmsg("couldn't create logfile: " RESPONSE_DUMP); - if(ptr) - free(ptr); - if(cmd) - free(cmd); + free(ptr); + free(cmd); return -1; } @@ -1110,28 +1102,22 @@ static int send_doc(curl_socket_t sock, struct httprequest *req) RESPONSE_DUMP, error, strerror(error)); if(got_exit_signal) { - if(ptr) - free(ptr); - if(cmd) - free(cmd); + free(ptr); + free(cmd); return -1; } if(sendfailure) { logmsg("Sending response failed. Only (%zu bytes) of (%zu bytes) were sent", responsesize-count, responsesize); - if(ptr) - free(ptr); - if(cmd) - free(cmd); + free(ptr); + free(cmd); return -1; } logmsg("Response sent (%zu bytes) and written to " RESPONSE_DUMP, responsesize); - - if(ptr) - free(ptr); + free(ptr); if(cmdsize > 0 ) { char command[32]; @@ -1169,9 +1155,7 @@ static int send_doc(curl_socket_t sock, struct httprequest *req) ptr = NULL; } while(ptr && *ptr); } - if(cmd) - free(cmd); - + free(cmd); req->open = persistant; prevtestno = req->testno; diff --git a/tests/server/sws.c b/tests/server/sws.c index 1adbeccda..094a92d64 100644 --- a/tests/server/sws.c +++ b/tests/server/sws.c @@ -442,8 +442,7 @@ static int parse_servercmd(struct httprequest *req) else break; } - if(orgcmd) - free(orgcmd); + free(orgcmd); } return 0; /* OK! */ @@ -1126,8 +1125,7 @@ static int send_doc(curl_socket_t sock, struct httprequest *req) } if(got_exit_signal) { - if(ptr) - free(ptr); + free(ptr); return -1; } @@ -1137,8 +1135,7 @@ static int send_doc(curl_socket_t sock, struct httprequest *req) error = errno; logmsg("fopen() failed with error: %d %s", error, strerror(error)); logmsg(" [4] Error opening file: %s", filename); - if(ptr) - free(ptr); + free(ptr); return 0; } else { @@ -1147,18 +1144,15 @@ static int send_doc(curl_socket_t sock, struct httprequest *req) fclose(stream); if(error) { logmsg("getpart() failed with error: %d", error); - if(ptr) - free(ptr); + free(ptr); return 0; } } } if(got_exit_signal) { - if(ptr) - free(ptr); - if(cmd) - free(cmd); + free(ptr); + free(cmd); return -1; } @@ -1181,10 +1175,8 @@ static int send_doc(curl_socket_t sock, struct httprequest *req) error = errno; logmsg("fopen() failed with error: %d %s", error, strerror(error)); logmsg(" [5] Error opening file: %s", responsedump); - if(ptr) - free(ptr); - if(cmd) - free(cmd); + free(ptr); + free(cmd); return -1; } @@ -1228,28 +1220,22 @@ static int send_doc(curl_socket_t sock, struct httprequest *req) responsedump, error, strerror(error)); if(got_exit_signal) { - if(ptr) - free(ptr); - if(cmd) - free(cmd); + free(ptr); + free(cmd); return -1; } if(sendfailure) { logmsg("Sending response failed. Only (%zu bytes) of (%zu bytes) were sent", responsesize-count, responsesize); - if(ptr) - free(ptr); - if(cmd) - free(cmd); + free(ptr); + free(cmd); return -1; } logmsg("Response sent (%zu bytes) and written to %s", responsesize, responsedump); - - if(ptr) - free(ptr); + free(ptr); if(cmdsize > 0 ) { char command[32]; @@ -1285,9 +1271,7 @@ static int send_doc(curl_socket_t sock, struct httprequest *req) ptr = NULL; } while(ptr && *ptr); } - if(cmd) - free(cmd); - + free(cmd); req->open = use_gopher?FALSE:persistant; prevtestno = req->testno; diff --git a/tests/server/tftpd.c b/tests/server/tftpd.c index 25557932f..cef129f2e 100644 --- a/tests/server/tftpd.c +++ b/tests/server/tftpd.c @@ -876,8 +876,7 @@ int main(int argc, char **argv) memset(&test, 0, sizeof(test)); if (do_tftp(&test, tp, n) < 0) break; - if(test.buffer) - free(test.buffer); + free(test.buffer); } sclose(peer); peer = CURL_SOCKET_BAD; @@ -1089,8 +1088,7 @@ static int parse_servercmd(struct testcase *req) else break; } - if(orgcmd) - free(orgcmd); + free(orgcmd); } return 0; /* OK! */ diff --git a/tests/unit/unit1305.c b/tests/unit/unit1305.c index 91e3b0b27..93815e5f8 100644 --- a/tests/unit/unit1305.c +++ b/tests/unit/unit1305.c @@ -65,9 +65,7 @@ static void unit_stop( void ) Curl_freeaddrinfo(data_node->addr); free(data_node); } - if (data_key) - free(data_key); - + free(data_key); Curl_hash_destroy(hp); curl_easy_cleanup(data); -- cgit v1.2.3