]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
strdup: do Curl_strndup without strncpy
authorDaniel Stenberg <daniel@haxx.se>
Sat, 4 Nov 2023 22:47:05 +0000 (23:47 +0100)
committerDaniel Stenberg <daniel@haxx.se>
Sun, 5 Nov 2023 22:21:18 +0000 (23:21 +0100)
To avoid (false positive) gcc-13 compiler warnings.

Follow-up to 4855debd8a2c1cb

Assisted-by: Jay Satiro
Reported-by: Viktor Szakats
Fixes #12258

lib/http.c
lib/strdup.c
lib/strdup.h

index d08d6cf01b0bcbc626eb3d95885f4454bbd5ec89..4c7059cd2a001f93610bf15931d0e2b51567bf7e 100644 (file)
@@ -4608,17 +4608,6 @@ out:
   return result;
 }
 
-/* simple implementation of strndup(), which isn't portable */
-static char *my_strndup(const char *ptr, size_t len)
-{
-  char *copy = malloc(len + 1);
-  if(!copy)
-    return NULL;
-  memcpy(copy, ptr, len);
-  copy[len] = '\0';
-  return copy;
-}
-
 CURLcode Curl_http_req_make(struct httpreq **preq,
                             const char *method, size_t m_len,
                             const char *scheme, size_t s_len,
@@ -4637,17 +4626,17 @@ CURLcode Curl_http_req_make(struct httpreq **preq,
     goto out;
   memcpy(req->method, method, m_len);
   if(scheme) {
-    req->scheme = my_strndup(scheme, s_len);
+    req->scheme = Curl_strndup(scheme, s_len);
     if(!req->scheme)
       goto out;
   }
   if(authority) {
-    req->authority = my_strndup(authority, a_len);
+    req->authority = Curl_strndup(authority, a_len);
     if(!req->authority)
       goto out;
   }
   if(path) {
-    req->path = my_strndup(path, p_len);
+    req->path = Curl_strndup(path, p_len);
     if(!req->path)
       goto out;
   }
index ea2b6d0c0a3a3b0d226338ea127b93e4312e1a4d..fc34889169427c40ddaae7948ad269a764cbcbe4 100644 (file)
@@ -104,17 +104,21 @@ void *Curl_memdup(const void *src, size_t length)
  * Curl_strndup(source, length)
  *
  * Copies the 'source' string to a newly allocated buffer (that is returned).
- * Copies not more than 'length' bytes then adds a null terminator.
+ * Copies not more than 'length' bytes (up to a null terminator) then adds a
+ * null terminator.
  *
  * Returns the new pointer or NULL on failure.
  *
  ***************************************************************************/
-void *Curl_strndup(const void *src, size_t length)
+void *Curl_strndup(const char *src, size_t length)
 {
-  char *buf = malloc(length + 1);
+  char *buf = memchr(src, '\0', length);
+  if(buf)
+    length = buf - src;
+  buf = malloc(length + 1);
   if(!buf)
     return NULL;
-  strncpy(buf, src, length);
+  memcpy(buf, src, length);
   buf[length] = 0;
   return buf;
 }
index 83dd4b9b53f675fb07c31c1735d28a3573ff6122..b646d17a4bcf727ee8eef834f9a9b2832a479818 100644 (file)
@@ -33,6 +33,6 @@ wchar_t* Curl_wcsdup(const wchar_t* src);
 #endif
 void *Curl_memdup(const void *src, size_t buffer_length);
 void *Curl_saferealloc(void *ptr, size_t size);
-void *Curl_strndup(const void *src, size_t length);
+void *Curl_strndup(const char *src, size_t length);
 
 #endif /* HEADER_CURL_STRDUP_H */