]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
hsts: use one malloc instead of two per entry
authorDaniel Stenberg <daniel@haxx.se>
Sun, 7 Dec 2025 11:24:33 +0000 (12:24 +0100)
committerDaniel Stenberg <daniel@haxx.se>
Sun, 7 Dec 2025 11:54:03 +0000 (12:54 +0100)
Closes #19861

lib/hsts.c
lib/hsts.h

index 66f983e493fba40f2da106758a144ddb32a019c9..62c4c587dfb334dfe5a7265d778bd9cb3eeb6ac9 100644 (file)
@@ -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;
 
index 91fe778788c717162809dad75ed6ea614c14706a..bd6ebba4dea448499c03bcfc99639a441ad84243 100644 (file)
@@ -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. */