From 8b8d7acc6e7086358b744032b9cf3643029acd5e Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Thu, 13 Apr 2023 10:04:04 +0200 Subject: [PATCH] http: skip a double NULL assign and also use a local variable to shorten the long names and increase readability in the function. Pointed out by PVS. Ref: #10929 Closes #10950 --- lib/http.c | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/lib/http.c b/lib/http.c index b2674e54a2..e6612c13d1 100644 --- a/lib/http.c +++ b/lib/http.c @@ -2163,6 +2163,7 @@ CURLcode Curl_http_useragent(struct Curl_easy *data) CURLcode Curl_http_host(struct Curl_easy *data, struct connectdata *conn) { const char *ptr; + struct dynamically_allocated_data *aptr = &data->state.aptr; if(!data->state.this_is_a_follow) { /* Free to avoid leaking memory on multiple requests */ free(data->state.first_host); @@ -2174,7 +2175,7 @@ CURLcode Curl_http_host(struct Curl_easy *data, struct connectdata *conn) data->state.first_remote_port = conn->remote_port; data->state.first_remote_protocol = conn->handler->protocol; } - Curl_safefree(data->state.aptr.host); + Curl_safefree(aptr->host); ptr = Curl_checkheaders(data, STRCONST("Host")); if(ptr && (!data->state.this_is_a_follow || @@ -2209,19 +2210,16 @@ CURLcode Curl_http_host(struct Curl_easy *data, struct connectdata *conn) if(colon) *colon = 0; /* The host must not include an embedded port number */ } - Curl_safefree(data->state.aptr.cookiehost); - data->state.aptr.cookiehost = cookiehost; + Curl_safefree(aptr->cookiehost); + aptr->cookiehost = cookiehost; } #endif if(strcmp("Host:", ptr)) { - data->state.aptr.host = aprintf("Host:%s\r\n", &ptr[5]); - if(!data->state.aptr.host) + aptr->host = aprintf("Host:%s\r\n", &ptr[5]); + if(!aptr->host) return CURLE_OUT_OF_MEMORY; } - else - /* when clearing the header */ - data->state.aptr.host = NULL; } else { /* When building Host: headers, we must put the host name within @@ -2234,18 +2232,14 @@ CURLcode Curl_http_host(struct Curl_easy *data, struct connectdata *conn) (conn->remote_port == PORT_HTTP)) ) /* if(HTTPS on port 443) OR (HTTP on port 80) then don't include the port number in the host string */ - data->state.aptr.host = aprintf("Host: %s%s%s\r\n", - conn->bits.ipv6_ip?"[":"", - host, - conn->bits.ipv6_ip?"]":""); + aptr->host = aprintf("Host: %s%s%s\r\n", conn->bits.ipv6_ip?"[":"", + host, conn->bits.ipv6_ip?"]":""); else - data->state.aptr.host = aprintf("Host: %s%s%s:%d\r\n", - conn->bits.ipv6_ip?"[":"", - host, - conn->bits.ipv6_ip?"]":"", - conn->remote_port); + aptr->host = aprintf("Host: %s%s%s:%d\r\n", conn->bits.ipv6_ip?"[":"", + host, conn->bits.ipv6_ip?"]":"", + conn->remote_port); - if(!data->state.aptr.host) + if(!aptr->host) /* without Host: we can't make a nice request */ return CURLE_OUT_OF_MEMORY; }