]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
lib: reduce memcpy calls
authorDaniel Stenberg <daniel@haxx.se>
Thu, 30 Oct 2025 13:42:03 +0000 (14:42 +0100)
committerDaniel Stenberg <daniel@haxx.se>
Thu, 30 Oct 2025 14:40:21 +0000 (15:40 +0100)
socks_gssapi: the malloc + memcpy was superflous and can be skipped

cleartext: avoid malloc + three memcpy with aprintf()

digest_sspi: use memdup0 instead of malloc + memcpy

vtls: use memdup0 instead of malloc + memcpy

Closes #19282

lib/socks_gssapi.c
lib/vauth/cleartext.c
lib/vauth/digest_sspi.c
lib/vtls/vtls.c

index 34380ae9a045cfcf6873910145709a5bd597f232..929132f570a3fdc399adfa7f0919e68f298472f0 100644 (file)
@@ -126,7 +126,6 @@ CURLcode Curl_SOCKS5_gssapi_negotiate(struct Curl_cfilter *cf,
   gss_name_t       server = GSS_C_NO_NAME;
   gss_name_t       gss_client_name = GSS_C_NO_NAME;
   unsigned short   us_length;
-  char             *user = NULL;
   unsigned char socksreq[4]; /* room for GSS-API exchange header only */
   const char *serviceptr = data->set.str[STRING_PROXY_SERVICE_NAME] ?
                            data->set.str[STRING_PROXY_SERVICE_NAME] : "rcmd";
@@ -327,21 +326,12 @@ CURLcode Curl_SOCKS5_gssapi_negotiate(struct Curl_cfilter *cf,
     failf(data, "Failed to determine username.");
     return CURLE_COULDNT_CONNECT;
   }
-  user = malloc(gss_send_token.length + 1);
-  if(!user) {
-    Curl_gss_delete_sec_context(&gss_status, &gss_context, NULL);
-    gss_release_name(&gss_status, &gss_client_name);
-    gss_release_buffer(&gss_status, &gss_send_token);
-    return CURLE_OUT_OF_MEMORY;
-  }
 
-  memcpy(user, gss_send_token.value, gss_send_token.length);
-  user[gss_send_token.length] = '\0';
+  infof(data, "SOCKS5 server authenticated user %.*s with GSS-API.",
+        (int)gss_send_token.length, (char *)gss_send_token.value);
+
   gss_release_name(&gss_status, &gss_client_name);
   gss_release_buffer(&gss_status, &gss_send_token);
-  infof(data, "SOCKS5 server authenticated user %s with GSS-API.",user);
-  free(user);
-  user = NULL;
 
   /* Do encryption */
   socksreq[0] = 1;    /* GSS-API subnegotiation version */
index dcfb13912d27bcdfaad6e309e755cfbf72d0f670..884ebce0f268ca58a3a99b628a724a7ce0e4a2fb 100644 (file)
@@ -62,35 +62,24 @@ CURLcode Curl_auth_create_plain_message(const char *authzid,
                                         const char *passwd,
                                         struct bufref *out)
 {
-  char *plainauth;
-  size_t plainlen;
-  size_t zlen;
-  size_t clen;
-  size_t plen;
+  size_t len;
+  char *auth;
 
-  zlen = (authzid == NULL ? 0 : strlen(authzid));
-  clen = strlen(authcid);
-  plen = strlen(passwd);
+  size_t zlen = (authzid == NULL ? 0 : strlen(authzid));
+  size_t clen = strlen(authcid);
+  size_t plen = strlen(passwd);
 
-  /* Compute binary message length. Check for overflows. */
-  if((zlen > SIZE_MAX/4) || (clen > SIZE_MAX/4) ||
-     (plen > (SIZE_MAX/2 - 2)))
-    return CURLE_OUT_OF_MEMORY;
-  plainlen = zlen + clen + plen + 2;
+  if((zlen > CURL_MAX_INPUT_LENGTH) || (clen > CURL_MAX_INPUT_LENGTH) ||
+     (plen > CURL_MAX_INPUT_LENGTH))
+    return CURLE_TOO_LARGE;
 
-  plainauth = malloc(plainlen + 1);
-  if(!plainauth)
-    return CURLE_OUT_OF_MEMORY;
+  len = zlen + clen + plen + 2;
 
-  /* Calculate the reply */
-  if(zlen)
-    memcpy(plainauth, authzid, zlen);
-  plainauth[zlen] = '\0';
-  memcpy(plainauth + zlen + 1, authcid, clen);
-  plainauth[zlen + clen + 1] = '\0';
-  memcpy(plainauth + zlen + clen + 2, passwd, plen);
-  plainauth[plainlen] = '\0';
-  Curl_bufref_set(out, plainauth, plainlen, curl_free);
+  auth = curl_maprintf("%s%c%s%c%s", authzid ? authzid : "", '\0',
+                       authcid, '\0', passwd);
+  if(!auth)
+    return CURLE_OUT_OF_MEMORY;
+  Curl_bufref_set(out, auth, len, curl_free);
   return CURLE_OK;
 }
 
index 5bf37705653a72d170b1dc7dcc40b2ca9fc1ff0d..9441ee26fcca1c056ea969db0f9a850c75f2a28b 100644 (file)
@@ -629,24 +629,15 @@ CURLcode Curl_auth_create_digest_http_message(struct Curl_easy *data,
     Curl_sspi_free_identity(p_identity);
   }
 
-  resp = malloc(output_token_len + 1);
+  resp = Curl_memdup0((const char *)output_token, output_token_len);
+  free(output_token);
   if(!resp) {
-    free(output_token);
-
     return CURLE_OUT_OF_MEMORY;
   }
 
-  /* Copy the generated response */
-  memcpy(resp, output_token, output_token_len);
-  resp[output_token_len] = 0;
-
   /* Return the response */
   *outptr = resp;
   *outlen = output_token_len;
-
-  /* Free the response buffer */
-  free(output_token);
-
   return CURLE_OK;
 }
 
index 3cd60e91b9aa3b1c5d48ced86c2d29c044c99b45..22d820b1602e9bf2e8ee475b1047eb757e8e98ed 100644 (file)
@@ -75,6 +75,7 @@
 #include "../select.h"
 #include "../setopt.h"
 #include "../rand.h"
+#include "../strdup.h"
 
 #ifdef USE_APPLE_SECTRUST
 #include <Security/Security.h>
@@ -2060,11 +2061,9 @@ CURLcode Curl_alpn_set_negotiated(struct Curl_cfilter *cf,
       result = CURLE_SSL_CONNECT_ERROR;
       goto out;
     }
-    connssl->negotiated.alpn = malloc(proto_len + 1);
+    connssl->negotiated.alpn = Curl_memdup0((const char *)proto, proto_len);
     if(!connssl->negotiated.alpn)
       return CURLE_OUT_OF_MEMORY;
-    memcpy(connssl->negotiated.alpn, proto, proto_len);
-    connssl->negotiated.alpn[proto_len] = 0;
   }
 
   if(proto && proto_len) {