From bff78cc18e8dc22926d4b0219e6fac08758ed790 Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Wed, 12 Oct 2011 21:32:10 +0200 Subject: [PATCH] OOM handling/cleanup slight adjustments --- lib/easy.c | 4 ++-- lib/ftp.c | 3 +-- lib/http.c | 6 ++++-- lib/transfer.c | 20 ++++++++++---------- lib/url.c | 49 +++++++++++++++++++++++++++++++++---------------- 5 files changed, 50 insertions(+), 32 deletions(-) diff --git a/lib/easy.c b/lib/easy.c index 9e06dc23b..60a55edf8 100644 --- a/lib/easy.c +++ b/lib/easy.c @@ -701,10 +701,10 @@ void curl_easy_reset(CURL *curl) struct SessionHandle *data = (struct SessionHandle *)curl; Curl_safefree(data->state.pathbuffer); - data->state.pathbuffer=NULL; + + data->state.path = NULL; Curl_safefree(data->state.proto.generic); - data->state.proto.generic=NULL; /* zero out UserDefined data: */ Curl_freeset(data); diff --git a/lib/ftp.c b/lib/ftp.c index b93361a2b..a078c5e71 100644 --- a/lib/ftp.c +++ b/lib/ftp.c @@ -3667,8 +3667,7 @@ static CURLcode wc_statemach(struct connectdata *conn) strcat(tmp_path, finfo->filename); /* switch default "state.pathbuffer" and tmp_path, good to see ftp_parse_url_path function to understand this trick */ - if(conn->data->state.pathbuffer) - free(conn->data->state.pathbuffer); + Curl_safefree(conn->data->state.pathbuffer); conn->data->state.pathbuffer = tmp_path; conn->data->state.path = tmp_path; diff --git a/lib/http.c b/lib/http.c index ee5d6116a..5b30f8aa0 100644 --- a/lib/http.c +++ b/lib/http.c @@ -1912,8 +1912,10 @@ CURLcode Curl_http(struct connectdata *conn, bool *done) memcpy(newurl + newlen + (ptr - url), ptr + currlen, /* copy the trailing zero byte too */ urllen - (ptr-url) - currlen + 1); - if(data->change.url_alloc) - free(data->change.url); + if(data->change.url_alloc) { + Curl_safefree(data->change.url); + data->change.url_alloc = FALSE; + } data->change.url = newurl; data->change.url_alloc = TRUE; } diff --git a/lib/transfer.c b/lib/transfer.c index 3d243674a..413a28082 100644 --- a/lib/transfer.c +++ b/lib/transfer.c @@ -1806,15 +1806,14 @@ CURLcode Curl_follow(struct SessionHandle *data, when we get the next URL. We pick the ->url field, which may or may not be 100% correct */ - if(data->change.referer_alloc) - /* If we already have an allocated referer, free this first */ - free(data->change.referer); + if(data->change.referer_alloc) { + Curl_safefree(data->change.referer); + data->change.referer_alloc = FALSE; + } data->change.referer = strdup(data->change.url); - if(!data->change.referer) { - data->change.referer_alloc = FALSE; + if(!data->change.referer) return CURLE_OUT_OF_MEMORY; - } data->change.referer_alloc = TRUE; /* yes, free this later */ } } @@ -1861,12 +1860,13 @@ CURLcode Curl_follow(struct SessionHandle *data, if(disallowport) data->state.allow_port = FALSE; - if(data->change.url_alloc) - free(data->change.url); - else - data->change.url_alloc = TRUE; /* the URL is allocated */ + if(data->change.url_alloc) { + Curl_safefree(data->change.url); + data->change.url_alloc = FALSE; + } data->change.url = newurl; + data->change.url_alloc = TRUE; newurl = NULL; /* don't free! */ infof(data, "Issue another request to this URL: '%s'\n", data->change.url); diff --git a/lib/url.c b/lib/url.c index 7eb59f5bb..5b00ca750 100644 --- a/lib/url.c +++ b/lib/url.c @@ -276,10 +276,7 @@ static CURLcode setstropt(char **charp, char * s) /* Release the previous storage at `charp' and replace by a dynamic storage copy of `s'. Return CURLE_OK or CURLE_OUT_OF_MEMORY. */ - if(*charp) { - free(*charp); - *charp = (char *) NULL; - } + Curl_safefree(*charp); if(s) { s = strdup(s); @@ -480,6 +477,8 @@ CURLcode Curl_close(struct SessionHandle *data) /* Free the pathbuffer */ Curl_safefree(data->state.pathbuffer); + data->state.path = NULL; + Curl_safefree(data->state.proto.generic); /* Close down all open SSL info and sessions */ @@ -488,11 +487,17 @@ CURLcode Curl_close(struct SessionHandle *data) Curl_safefree(data->state.scratch); Curl_ssl_free_certinfo(data); - if(data->change.referer_alloc) - free(data->change.referer); + if(data->change.referer_alloc) { + Curl_safefree(data->change.referer); + data->change.referer_alloc = FALSE; + } + data->change.referer = NULL; - if(data->change.url_alloc) - free(data->change.url); + if(data->change.url_alloc) { + Curl_safefree(data->change.url); + data->change.url_alloc = FALSE; + } + data->change.url = NULL; Curl_safefree(data->state.headerbuff); @@ -1216,7 +1221,7 @@ CURLcode Curl_setopt(struct SessionHandle *data, CURLoption option, * String to set in the HTTP Referer: field. */ if(data->change.referer_alloc) { - free(data->change.referer); + Curl_safefree(data->change.referer); data->change.referer_alloc = FALSE; } result = setstropt(&data->set.str[STRING_SET_REFERER], @@ -1633,8 +1638,8 @@ CURLcode Curl_setopt(struct SessionHandle *data, CURLoption option, */ if(data->change.url_alloc) { /* the already set URL is allocated, free it first! */ - free(data->change.url); - data->change.url_alloc=FALSE; + Curl_safefree(data->change.url); + data->change.url_alloc = FALSE; } result = setstropt(&data->set.str[STRING_SET_URL], va_arg(param, char *)); @@ -4428,8 +4433,10 @@ static CURLcode parse_remote_port(struct SessionHandle *data, if(!url) return CURLE_OUT_OF_MEMORY; - if(data->change.url_alloc) - free(data->change.url); + if(data->change.url_alloc) { + Curl_safefree(data->change.url); + data->change.url_alloc = FALSE; + } data->change.url = url; data->change.url_alloc = TRUE; @@ -4653,11 +4660,11 @@ static void reuse_conn(struct connectdata *old_conn, /* host can change, when doing keepalive with a proxy ! */ if(conn->bits.proxy) { - free(conn->host.rawalloc); + Curl_safefree(conn->host.rawalloc); conn->host=old_conn->host; } else - free(old_conn->host.rawalloc); /* free the newly allocated name buffer */ + Curl_safefree(old_conn->host.rawalloc); /* free the newly allocated name buffer */ /* persist connection info in session handle */ Curl_persistconninfo(conn); @@ -4756,14 +4763,19 @@ static CURLcode create_conn(struct SessionHandle *data, */ Curl_safefree(data->state.pathbuffer); + data->state.path = NULL; + data->state.pathbuffer = malloc(urllen+2); if(NULL == data->state.pathbuffer) return CURLE_OUT_OF_MEMORY; /* really bad error */ data->state.path = data->state.pathbuffer; conn->host.rawalloc = malloc(urllen+2); - if(NULL == conn->host.rawalloc) + if(NULL == conn->host.rawalloc) { + Curl_safefree(data->state.pathbuffer); + data->state.path = NULL; return CURLE_OUT_OF_MEMORY; + } conn->host.name = conn->host.rawalloc; conn->host.name[0] = 0; @@ -4788,6 +4800,11 @@ static CURLcode create_conn(struct SessionHandle *data, return CURLE_OUT_OF_MEMORY; } + if(data->change.url_alloc) { + Curl_safefree(data->change.url); + data->change.url_alloc = FALSE; + } + data->change.url = reurl; data->change.url_alloc = TRUE; /* free this later */ }