summaryrefslogtreecommitdiff
path: root/lib/doh.c
diff options
context:
space:
mode:
authorDaniel Stenberg <daniel@haxx.se>2020-06-06 23:10:18 +0200
committerDaniel Stenberg <daniel@haxx.se>2020-06-08 11:12:28 +0200
commit12c178b734a0f00ef5ea708e7f9dad81ee2cb4c1 (patch)
treee90e8b7bdfdccacab6ef41b58342a76359778886 /lib/doh.c
parent99e09d9046bfc7b13c09022248d17e5daf08e0e0 (diff)
downloadcurl-bagder/curl_he2ai-lessmalloc.tar.gz
Curl_addrinfo: use one malloc instead of threebagder/curl_he2ai-lessmalloc
To reduce the amount of allocations needed for creating a Curl_addrinfo struct, make one larger malloc instead of three separate smaller ones. Closes #5533
Diffstat (limited to 'lib/doh.c')
-rw-r--r--lib/doh.c19
1 files changed, 5 insertions, 14 deletions
diff --git a/lib/doh.c b/lib/doh.c
index 8f9e42c33..ebb2c243b 100644
--- a/lib/doh.c
+++ b/lib/doh.c
@@ -803,6 +803,7 @@ doh2ai(const struct dohentry *de, const char *hostname, int port)
#endif
CURLcode result = CURLE_OK;
int i;
+ size_t hostlen = strlen(hostname) + 1; /* include zero terminator */
if(!de)
/* no input == no output! */
@@ -825,24 +826,14 @@ doh2ai(const struct dohentry *de, const char *hostname, int port)
addrtype = AF_INET;
}
- ai = calloc(1, sizeof(struct Curl_addrinfo));
+ ai = calloc(1, sizeof(struct Curl_addrinfo) + ss_size + hostlen);
if(!ai) {
result = CURLE_OUT_OF_MEMORY;
break;
}
- ai->ai_canonname = strdup(hostname);
- if(!ai->ai_canonname) {
- result = CURLE_OUT_OF_MEMORY;
- free(ai);
- break;
- }
- ai->ai_addr = calloc(1, ss_size);
- if(!ai->ai_addr) {
- result = CURLE_OUT_OF_MEMORY;
- free(ai->ai_canonname);
- free(ai);
- break;
- }
+ ai->ai_addr = (void *)((char *)ai + sizeof(struct Curl_addrinfo));
+ ai->ai_canonname = (void *)((char *)ai->ai_addr + ss_size);
+ memcpy(ai->ai_canonname, hostname, hostlen);
if(!firstai)
/* store the pointer we want to return from this function */