From: Daniel Stenberg Date: Sun, 7 Dec 2025 11:24:33 +0000 (+0100) Subject: hsts: use one malloc instead of two per entry X-Git-Tag: rc-8_18_0-2~111 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9ec63d8565c9d3194c70e2ad3433b623308ee467;p=thirdparty%2Fcurl.git hsts: use one malloc instead of two per entry Closes #19861 --- diff --git a/lib/hsts.c b/lib/hsts.c index 66f983e493..62c4c587df 100644 --- a/lib/hsts.c +++ b/lib/hsts.c @@ -75,11 +75,7 @@ struct hsts *Curl_hsts_init(void) return h; } -static void hsts_free(struct stsentry *e) -{ - curlx_free(CURL_UNCONST(e->host)); - curlx_free(e); -} +#define hsts_free(x) curlx_free(x) void Curl_hsts_cleanup(struct hsts **hp) { @@ -111,18 +107,11 @@ static CURLcode hsts_create(struct hsts *h, /* strip off any trailing dot */ --hlen; if(hlen) { - char *duphost; - struct stsentry *sts = curlx_calloc(1, sizeof(struct stsentry)); + struct stsentry *sts = curlx_calloc(1, sizeof(struct stsentry) + hlen); if(!sts) return CURLE_OUT_OF_MEMORY; - - duphost = Curl_memdup0(hostname, hlen); - if(!duphost) { - curlx_free(sts); - return CURLE_OUT_OF_MEMORY; - } - - sts->host = duphost; + /* the null terminator is already there */ + memcpy(sts->host, hostname, hlen); sts->expires = expires; sts->includeSubDomains = subdomains; Curl_llist_append(&h->list, sts, &sts->node); @@ -294,7 +283,7 @@ static CURLcode hsts_push(struct Curl_easy *data, struct tm stamp; CURLcode result; - e.name = (char *)CURL_UNCONST(sts->host); + e.name = (char *)sts->host; e.namelen = strlen(sts->host); e.includeSubDomains = sts->includeSubDomains; diff --git a/lib/hsts.h b/lib/hsts.h index 91fe778788..bd6ebba4de 100644 --- a/lib/hsts.h +++ b/lib/hsts.h @@ -35,9 +35,9 @@ extern time_t deltatime; struct stsentry { struct Curl_llist_node node; - const char *host; curl_off_t expires; /* the timestamp of this entry's expiry */ BIT(includeSubDomains); + char host[1]; }; /* The HSTS cache. Needs to be able to tailmatch hostnames. */