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 --- lib/asyn-ares.c | 3 +-- lib/asyn-thread.c | 7 ++----- lib/base64.c | 3 +-- lib/cookie.c | 53 +++++++++++++++++------------------------------------ lib/curl_addrinfo.c | 9 ++------- lib/curl_ntlm_wb.c | 6 ++---- lib/escape.c | 3 +-- lib/formdata.c | 12 +++++------- lib/ftp.c | 28 +++++++++------------------- lib/ftplistparser.c | 3 +-- lib/getinfo.c | 3 +-- lib/http.c | 23 ++++++++--------------- lib/http_proxy.c | 13 ++++--------- lib/ldap.c | 7 ++----- lib/multi.c | 12 ++++-------- lib/nwlib.c | 8 ++------ lib/pingpong.c | 6 ++---- lib/security.c | 6 ++---- lib/url.c | 46 ++++++++++++++++------------------------------ lib/vtls/nss.c | 6 ++---- lib/vtls/vtls.c | 3 +-- lib/wildcard.c | 13 ++++--------- lib/x509asn1.c | 3 +-- 23 files changed, 90 insertions(+), 186 deletions(-) (limited to 'lib') diff --git a/lib/asyn-ares.c b/lib/asyn-ares.c index e04b21a69..c5ec48dbb 100644 --- a/lib/asyn-ares.c +++ b/lib/asyn-ares.c @@ -186,8 +186,7 @@ void Curl_resolver_cancel(struct connectdata *conn) */ static void destroy_async_data (struct Curl_async *async) { - if(async->hostname) - free(async->hostname); + free(async->hostname); if(async->os_specific) { struct ResolverResults *res = (struct ResolverResults *)async->os_specific; diff --git a/lib/asyn-thread.c b/lib/asyn-thread.c index 06631f09b..1d782b8ca 100644 --- a/lib/asyn-thread.c +++ b/lib/asyn-thread.c @@ -190,8 +190,7 @@ void destroy_thread_sync_data(struct thread_sync_data * tsd) free(tsd->mtx); } - if(tsd->hostname) - free(tsd->hostname); + free(tsd->hostname); if(tsd->res) Curl_freeaddrinfo(tsd->res); @@ -364,9 +363,7 @@ static void destroy_async_data (struct Curl_async *async) } async->os_specific = NULL; - if(async->hostname) - free(async->hostname); - + free(async->hostname); async->hostname = NULL; } diff --git a/lib/base64.c b/lib/base64.c index e230b73e8..648ee1e5d 100644 --- a/lib/base64.c +++ b/lib/base64.c @@ -252,8 +252,7 @@ static CURLcode base64_encode(const char *table64, *output = '\0'; *outptr = base64data; /* return pointer to new data, allocated memory */ - if(convbuf) - free(convbuf); + free(convbuf); *outlen = strlen(base64data); /* return the length of the new data */ diff --git a/lib/cookie.c b/lib/cookie.c index 601ff2a77..b71eab43f 100644 --- a/lib/cookie.c +++ b/lib/cookie.c @@ -103,23 +103,14 @@ Example set of cookies: static void freecookie(struct Cookie *co) { - if(co->expirestr) - free(co->expirestr); - if(co->domain) - free(co->domain); - if(co->path) - free(co->path); - if(co->spath) - free(co->spath); - if(co->name) - free(co->name); - if(co->value) - free(co->value); - if(co->maxage) - free(co->maxage); - if(co->version) - free(co->version); - + free(co->expirestr); + free(co->domain); + free(co->path); + free(co->spath); + free(co->name); + free(co->value); + free(co->maxage); + free(co->version); free(co); } @@ -296,8 +287,7 @@ void Curl_cookie_loadfiles(struct SessionHandle *data) */ static void strstore(char **str, const char *newstr) { - if(*str) - free(*str); + free(*str); *str = strdup(newstr); } @@ -832,21 +822,13 @@ Curl_cookie_add(struct SessionHandle *data, /* then free all the old pointers */ free(clist->name); - if(clist->value) - free(clist->value); - if(clist->domain) - free(clist->domain); - if(clist->path) - free(clist->path); - if(clist->spath) - free(clist->spath); - if(clist->expirestr) - free(clist->expirestr); - - if(clist->version) - free(clist->version); - if(clist->maxage) - free(clist->maxage); + free(clist->value); + free(clist->domain); + free(clist->path); + free(clist->spath); + free(clist->expirestr); + free(clist->version); + free(clist->maxage); *clist = *co; /* then store all the new data */ @@ -1214,8 +1196,7 @@ void Curl_cookie_clearsess(struct CookieInfo *cookies) void Curl_cookie_cleanup(struct CookieInfo *c) { if(c) { - if(c->filename) - free(c->filename); + free(c->filename); Curl_cookie_freelist(c->cookies, TRUE); free(c); /* free the base struct as well */ } diff --git a/lib/curl_addrinfo.c b/lib/curl_addrinfo.c index bf39906d4..6627a6b96 100644 --- a/lib/curl_addrinfo.c +++ b/lib/curl_addrinfo.c @@ -80,13 +80,8 @@ Curl_freeaddrinfo(Curl_addrinfo *cahead) Curl_addrinfo *ca; for(ca = cahead; ca != NULL; ca = canext) { - - if(ca->ai_addr) - free(ca->ai_addr); - - if(ca->ai_canonname) - free(ca->ai_canonname); - + free(ca->ai_addr); + free(ca->ai_canonname); canext = ca->ai_next; free(ca); diff --git a/lib/curl_ntlm_wb.c b/lib/curl_ntlm_wb.c index a741d52bb..c5288446f 100644 --- a/lib/curl_ntlm_wb.c +++ b/lib/curl_ntlm_wb.c @@ -419,10 +419,8 @@ CURLcode Curl_output_ntlm_wb(struct connectdata *conn, case NTLMSTATE_TYPE3: /* connection is already authenticated, * don't send a header in future requests */ - if(*allocuserpwd) { - free(*allocuserpwd); - *allocuserpwd=NULL; - } + free(*allocuserpwd); + *allocuserpwd=NULL; authp->done = TRUE; break; } diff --git a/lib/escape.c b/lib/escape.c index 5a9b8f438..814e5b72f 100644 --- a/lib/escape.c +++ b/lib/escape.c @@ -227,6 +227,5 @@ char *curl_easy_unescape(CURL *handle, const char *string, int length, the library's memory system */ void curl_free(void *p) { - if(p) - free(p); + free(p); } diff --git a/lib/formdata.c b/lib/formdata.c index 2cadb0df8..3f41a4275 100644 --- a/lib/formdata.c +++ b/lib/formdata.c @@ -972,16 +972,14 @@ void curl_formfree(struct curl_httppost *form) if(form->more) curl_formfree(form->more); - if(!(form->flags & HTTPPOST_PTRNAME) && form->name) + if(!(form->flags & HTTPPOST_PTRNAME)) free(form->name); /* free the name */ if(!(form->flags & - (HTTPPOST_PTRCONTENTS|HTTPPOST_BUFFER|HTTPPOST_CALLBACK)) && - form->contents) + (HTTPPOST_PTRCONTENTS|HTTPPOST_BUFFER|HTTPPOST_CALLBACK)) + ) free(form->contents); /* free the contents */ - if(form->contenttype) - free(form->contenttype); /* free the content type */ - if(form->showfilename) - free(form->showfilename); /* free the faked file name */ + free(form->contenttype); /* free the content type */ + free(form->showfilename); /* free the faked file name */ free(form); /* free the struct */ } while((form = next) != NULL); /* continue */ diff --git a/lib/ftp.c b/lib/ftp.c index 128033db4..461045606 100644 --- a/lib/ftp.c +++ b/lib/ftp.c @@ -283,10 +283,8 @@ static void freedirs(struct ftp_conn *ftpc) int i; if(ftpc->dirs) { for(i=0; i < ftpc->dirdepth; i++) { - if(ftpc->dirs[i]) { - free(ftpc->dirs[i]); - ftpc->dirs[i]=NULL; - } + free(ftpc->dirs[i]); + ftpc->dirs[i]=NULL; } free(ftpc->dirs); ftpc->dirs = NULL; @@ -1523,16 +1521,13 @@ static CURLcode ftp_state_list(struct connectdata *conn) lstArg? lstArg: "" ); if(!cmd) { - if(lstArg) - free(lstArg); + free(lstArg); return CURLE_OUT_OF_MEMORY; } result = Curl_pp_sendf(&conn->proto.ftpc.pp, "%s", cmd); - if(lstArg) - free(lstArg); - + free(lstArg); free(cmd); if(result) @@ -3266,8 +3261,7 @@ static CURLcode ftp_done(struct connectdata *conn, CURLcode status, } /* now store a copy of the directory we are in */ - if(ftpc->prevpath) - free(ftpc->prevpath); + free(ftpc->prevpath); if(data->set.wildcardmatch) { if(data->set.chunk_end && ftpc->file) { @@ -4192,14 +4186,10 @@ static CURLcode ftp_disconnect(struct connectdata *conn, bool dead_connection) } freedirs(ftpc); - if(ftpc->prevpath) { - free(ftpc->prevpath); - ftpc->prevpath = NULL; - } - if(ftpc->server_os) { - free(ftpc->server_os); - ftpc->server_os = NULL; - } + free(ftpc->prevpath); + ftpc->prevpath = NULL; + free(ftpc->server_os); + ftpc->server_os = NULL; Curl_pp_disconnect(pp); diff --git a/lib/ftplistparser.c b/lib/ftplistparser.c index ea8ec9694..17e0a66f8 100644 --- a/lib/ftplistparser.c +++ b/lib/ftplistparser.c @@ -187,8 +187,7 @@ struct ftp_parselist_data *Curl_ftp_parselist_data_alloc(void) void Curl_ftp_parselist_data_free(struct ftp_parselist_data **pl_data) { - if(*pl_data) - free(*pl_data); + free(*pl_data); *pl_data = NULL; } diff --git a/lib/getinfo.c b/lib/getinfo.c index 8395ee934..9ece7760c 100644 --- a/lib/getinfo.c +++ b/lib/getinfo.c @@ -58,8 +58,7 @@ CURLcode Curl_initinfo(struct SessionHandle *data) info->filetime = -1; /* -1 is an illegal time and thus means unknown */ info->timecond = FALSE; - if(info->contenttype) - free(info->contenttype); + free(info->contenttype); info->contenttype = NULL; info->header_size = 0; diff --git a/lib/http.c b/lib/http.c index 74c9b5737..2ad330592 100644 --- a/lib/http.c +++ b/lib/http.c @@ -1124,8 +1124,7 @@ CURLcode Curl_add_buffer_send(Curl_send_buffer *in, /* Curl_convert_to_network calls failf if unsuccessful */ if(result) { /* conversion failed, free memory and return to the caller */ - if(in->buffer) - free(in->buffer); + free(in->buffer); free(in); return result; } @@ -1228,8 +1227,7 @@ CURLcode Curl_add_buffer_send(Curl_send_buffer *in, conn->writechannel_inuse = FALSE; } } - if(in->buffer) - free(in->buffer); + free(in->buffer); free(in); return result; @@ -1253,8 +1251,7 @@ CURLcode Curl_add_bufferf(Curl_send_buffer *in, const char *fmt, ...) return result; } /* If we failed, we cleanup the whole buffer and return error */ - if(in->buffer) - free(in->buffer); + free(in->buffer); free(in); return CURLE_OUT_OF_MEMORY; } @@ -1826,10 +1823,8 @@ CURLcode Curl_http(struct connectdata *conn, bool *done) http = data->req.protop; if(!data->state.this_is_a_follow) { - /* this is not a followed location, get the original host name */ - if(data->state.first_host) - /* Free to avoid leaking memory on multiple requests*/ - free(data->state.first_host); + /* Free to avoid leaking memory on multiple requests*/ + free(data->state.first_host); data->state.first_host = strdup(conn->host.name); if(!data->state.first_host) @@ -1873,7 +1868,7 @@ CURLcode Curl_http(struct connectdata *conn, bool *done) it might have been used in the proxy connect, but if we have got a header with the user-agent string specified, we erase the previously made string here. */ - if(Curl_checkheaders(conn, "User-Agent:") && conn->allocptr.uagent) { + if(Curl_checkheaders(conn, "User-Agent:")) { free(conn->allocptr.uagent); conn->allocptr.uagent=NULL; } @@ -2218,8 +2213,7 @@ CURLcode Curl_http(struct connectdata *conn, bool *done) if(((httpreq == HTTPREQ_GET) || (httpreq == HTTPREQ_HEAD)) && !Curl_checkheaders(conn, "Range:")) { /* if a line like this was already allocated, free the previous one */ - if(conn->allocptr.rangeline) - free(conn->allocptr.rangeline); + free(conn->allocptr.rangeline); conn->allocptr.rangeline = aprintf("Range: bytes=%s\r\n", data->state.range); } @@ -2227,8 +2221,7 @@ CURLcode Curl_http(struct connectdata *conn, bool *done) !Curl_checkheaders(conn, "Content-Range:")) { /* if a line like this was already allocated, free the previous one */ - if(conn->allocptr.rangeline) - free(conn->allocptr.rangeline); + free(conn->allocptr.rangeline); if(data->set.set_resume_from < 0) { /* Upload resume was asked for, but we don't know the size of the diff --git a/lib/http_proxy.c b/lib/http_proxy.c index fd8ccee31..85ef1a2cc 100644 --- a/lib/http_proxy.c +++ b/lib/http_proxy.c @@ -121,13 +121,11 @@ CURLcode Curl_proxyCONNECT(struct connectdata *conn, infof(data, "Establish HTTP proxy tunnel to %s:%hu\n", hostname, remote_port); - if(data->req.newurl) { /* This only happens if we've looped here due to authentication reasons, and we don't really use the newly cloned URL here then. Just free() it. */ - free(data->req.newurl); - data->req.newurl = NULL; - } + free(data->req.newurl); + data->req.newurl = NULL; /* initialize a dynamic send-buffer */ req_buffer = Curl_add_buffer_init(); @@ -552,11 +550,8 @@ CURLcode Curl_proxyCONNECT(struct connectdata *conn, infof(data, "Connect me again please\n"); } else { - if(data->req.newurl) { - /* this won't be used anymore for the CONNECT so free it now */ - free(data->req.newurl); - data->req.newurl = NULL; - } + free(data->req.newurl); + data->req.newurl = NULL; /* failure, close this connection to avoid re-use */ connclose(conn, "proxy CONNECT failure"); Curl_closesocket(conn, conn->sock[sockindex]); diff --git a/lib/ldap.c b/lib/ldap.c index e2de2446c..8434f7896 100644 --- a/lib/ldap.c +++ b/lib/ldap.c @@ -996,11 +996,8 @@ static void _ldap_free_urldesc (LDAPURLDesc *ludp) if(!ludp) return; - if(ludp->lud_dn) - free(ludp->lud_dn); - - if(ludp->lud_filter) - free(ludp->lud_filter); + free(ludp->lud_dn); + free(ludp->lud_filter); if(ludp->lud_attrs) { for(i = 0; i < ludp->lud_attrs_dups; i++) diff --git a/lib/multi.c b/lib/multi.c index 6d21b138d..88e81c631 100644 --- a/lib/multi.c +++ b/lib/multi.c @@ -217,8 +217,7 @@ static void sh_freeentry(void *freethis) { struct Curl_sh_entry *p = (struct Curl_sh_entry *) freethis; - if(p) - free(p); + free(p); } static size_t fd_key_compare(void *k1, size_t k1_len, void *k2, size_t k2_len) @@ -1582,8 +1581,7 @@ static CURLMcode multi_runsingle(struct Curl_multi *multi, if(!retry) { /* if the URL is a follow-location and not just a retried request then figure out the URL here */ - if(newurl) - free(newurl); + free(newurl); newurl = data->req.newurl; data->req.newurl = NULL; follow = FOLLOW_REDIR; @@ -1608,8 +1606,7 @@ static CURLMcode multi_runsingle(struct Curl_multi *multi, /* but first check to see if we got a location info even though we're not following redirects */ if(data->req.location) { - if(newurl) - free(newurl); + free(newurl); newurl = data->req.location; data->req.location = NULL; result = Curl_follow(data, newurl, FOLLOW_FAKE); @@ -1624,8 +1621,7 @@ static CURLMcode multi_runsingle(struct Curl_multi *multi, } } - if(newurl) - free(newurl); + free(newurl); break; } diff --git a/lib/nwlib.c b/lib/nwlib.c index 252bf11ec..bd3f27eac 100644 --- a/lib/nwlib.c +++ b/lib/nwlib.c @@ -282,9 +282,7 @@ int DisposeLibraryData( void *data ) if(data) { void *tenbytes = ((libdata_t *) data)->tenbytes; - if(tenbytes) - free(tenbytes); - + free(tenbytes); free(data); } @@ -296,9 +294,7 @@ void DisposeThreadData( void *data ) if(data) { void *twentybytes = ((libthreaddata_t *) data)->twentybytes; - if(twentybytes) - free(twentybytes); - + free(twentybytes); free(data); } } diff --git a/lib/pingpong.c b/lib/pingpong.c index 20c58f457..167079272 100644 --- a/lib/pingpong.c +++ b/lib/pingpong.c @@ -493,10 +493,8 @@ CURLcode Curl_pp_flushsend(struct pingpong *pp) CURLcode Curl_pp_disconnect(struct pingpong *pp) { - if(pp->cache) { - free(pp->cache); - pp->cache = NULL; - } + free(pp->cache); + pp->cache = NULL; return CURLE_OK; } diff --git a/lib/security.c b/lib/security.c index 70198ac05..ccaa1f6e9 100644 --- a/lib/security.c +++ b/lib/security.c @@ -577,10 +577,8 @@ Curl_sec_end(struct connectdata *conn) { if(conn->mech != NULL && conn->mech->end) conn->mech->end(conn->app_data); - if(conn->app_data) { - free(conn->app_data); - conn->app_data = NULL; - } + free(conn->app_data); + conn->app_data = NULL; if(conn->in_buffer.data) { free(conn->in_buffer.data); conn->in_buffer.data = NULL; diff --git a/lib/url.c b/lib/url.c index a3693edab..eec11a0e3 100644 --- a/lib/url.c +++ b/lib/url.c @@ -448,10 +448,8 @@ CURLcode Curl_close(struct SessionHandle *data) Curl_ssl_free_certinfo(data); /* Cleanup possible redirect junk */ - if(data->req.newurl) { - free(data->req.newurl); - data->req.newurl = NULL; - } + free(data->req.newurl); + data->req.newurl = NULL; if(data->change.referer_alloc) { Curl_safefree(data->change.referer); @@ -668,8 +666,7 @@ CURLcode Curl_open(struct SessionHandle **curl) if(result) { Curl_resolver_cleanup(data->state.resolver); - if(data->state.headerbuff) - free(data->state.headerbuff); + free(data->state.headerbuff); Curl_freeset(data); free(data); data = NULL; @@ -2731,10 +2728,9 @@ CURLcode Curl_disconnect(struct connectdata *conn, bool dead_connection) free(conn->host.encalloc); /* encoded host name buffer, must be freed with idn_free() since this was allocated by curl_win32_idn_to_ascii */ - if(conn->proxy.encalloc) - free(conn->proxy.encalloc); /* encoded proxy name buffer, must be freed - with idn_free() since this was allocated by - curl_win32_idn_to_ascii */ + free(conn->proxy.encalloc); /* encoded proxy name buffer, must be freed + with idn_free() since this was allocated by + curl_win32_idn_to_ascii */ #endif Curl_ssl_close(conn, FIRSTSOCKET); @@ -4425,8 +4421,7 @@ static char *detect_proxy(struct connectdata *conn) } } /* if(!check_noproxy(conn->host.name, no_proxy)) - it wasn't specified non-proxy */ - if(no_proxy) - free(no_proxy); + free(no_proxy); #else /* !CURL_DISABLE_HTTP */ @@ -5189,8 +5184,7 @@ static CURLcode resolve_server(struct SessionHandle *data, static void reuse_conn(struct connectdata *old_conn, struct connectdata *conn) { - if(old_conn->proxy.rawalloc) - free(old_conn->proxy.rawalloc); + free(old_conn->proxy.rawalloc); /* free the SSL config struct from this connection struct as this was allocated in vain and is targeted for destruction */ @@ -5439,10 +5433,8 @@ static CURLcode create_conn(struct SessionHandle *data, if(data->set.str[STRING_NOPROXY] && check_noproxy(conn->host.name, data->set.str[STRING_NOPROXY])) { - if(proxy) { - free(proxy); /* proxy is in exception list */ - proxy = NULL; - } + free(proxy); /* proxy is in exception list */ + proxy = NULL; } else if(!proxy) proxy = detect_proxy(conn); @@ -5931,14 +5923,10 @@ CURLcode Curl_done(struct connectdata **connp, conn->bits.done = TRUE; /* called just now! */ /* Cleanup possible redirect junk */ - if(data->req.newurl) { - free(data->req.newurl); - data->req.newurl = NULL; - } - if(data->req.location) { - free(data->req.location); - data->req.location = NULL; - } + free(data->req.newurl); + data->req.newurl = NULL; + free(data->req.location); + data->req.location = NULL; Curl_resolver_cancel(conn); @@ -5971,10 +5959,8 @@ CURLcode Curl_done(struct connectdata **connp, /* if the transfer was completed in a paused state there can be buffered data left to write and then kill */ - if(data->state.tempwrite) { - free(data->state.tempwrite); - data->state.tempwrite = NULL; - } + free(data->state.tempwrite); + data->state.tempwrite = NULL; /* if data->set.reuse_forbid is TRUE, it means the libcurl client has forced us to close this connection. This is ignored for requests taking diff --git a/lib/vtls/nss.c b/lib/vtls/nss.c index d1309dd04..82fab8597 100644 --- a/lib/vtls/nss.c +++ b/lib/vtls/nss.c @@ -1245,10 +1245,8 @@ void Curl_nss_close(struct connectdata *conn, int sockindex) * authentication data from a previous connection. */ SSL_InvalidateSession(connssl->handle); - if(connssl->client_nickname != NULL) { - free(connssl->client_nickname); - connssl->client_nickname = NULL; - } + free(connssl->client_nickname); + connssl->client_nickname = NULL; /* destroy all NSS objects in order to avoid failure of NSS shutdown */ Curl_llist_destroy(connssl->obj_list, NULL); connssl->obj_list = NULL; diff --git a/lib/vtls/vtls.c b/lib/vtls/vtls.c index c411b9a73..2230a0433 100644 --- a/lib/vtls/vtls.c +++ b/lib/vtls/vtls.c @@ -463,9 +463,8 @@ CURLcode Curl_ssl_addsessionid(struct connectdata *conn, store->sessionid = ssl_sessionid; store->idsize = idsize; store->age = *general_age; /* set current age */ - if(store->name) /* free it if there's one already present */ - free(store->name); + free(store->name); store->name = clone_host; /* clone host name */ store->remote_port = conn->remote_port; /* port number */ diff --git a/lib/wildcard.c b/lib/wildcard.c index 68be60801..6f55839db 100644 --- a/lib/wildcard.c +++ b/lib/wildcard.c @@ -59,15 +59,10 @@ void Curl_wildcard_dtor(struct WildcardData *wc) wc->filelist = NULL; } - if(wc->path) { - free(wc->path); - wc->path = NULL; - } - - if(wc->pattern) { - free(wc->pattern); - wc->pattern = NULL; - } + free(wc->path); + wc->path = NULL; + free(wc->pattern); + wc->pattern = NULL; wc->customptr = NULL; wc->state = CURLWC_INIT; diff --git a/lib/x509asn1.c b/lib/x509asn1.c index 32f1ea20c..4d50f0e0c 100644 --- a/lib/x509asn1.c +++ b/lib/x509asn1.c @@ -1116,8 +1116,7 @@ CURLcode Curl_verifyhost(struct connectdata * conn, if(len > 0) if(strlen(dnsname) == (size_t) len) i = Curl_cert_hostcheck((const char *) dnsname, conn->host.name); - if(dnsname) - free(dnsname); + free(dnsname); if(!i) return CURLE_PEER_FAILED_VERIFICATION; matched = i; -- cgit v1.2.3