]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
tool: replace three malloc + copy with memdup0
authorDaniel Stenberg <daniel@haxx.se>
Tue, 5 Aug 2025 12:24:15 +0000 (14:24 +0200)
committerDaniel Stenberg <daniel@haxx.se>
Tue, 5 Aug 2025 13:46:51 +0000 (15:46 +0200)
The function already existed for private use in var.c

Closes #18185

src/tool_cb_hdr.c
src/tool_strdup.c
src/tool_strdup.h
src/tool_urlglob.c
src/var.c

index e988224ac8580236f4e7c2e9d7efe9d73c63bb6d..85f74cf16dfb64a2f94f847952e3ed855f99ac4a 100644 (file)
@@ -34,6 +34,7 @@
 #include "tool_cb_wrt.h"
 #include "tool_operate.h"
 #include "tool_libinfo.h"
+#include "tool_strdup.h"
 
 #include "memdebug.h" /* keep this as LAST include */
 
@@ -322,12 +323,9 @@ static char *parse_filename(const char *ptr, size_t len)
   char *q;
   char  stop = '\0';
 
-  /* simple implementation of strndup() */
-  copy = malloc(len + 1);
+  copy = memdup0(ptr, len);
   if(!copy)
     return NULL;
-  memcpy(copy, ptr, len);
-  copy[len] = '\0';
 
   p = copy;
   if(*p == '\'' || *p == '"') {
@@ -438,11 +436,9 @@ static void write_linked_location(CURL *curl, const char *location,
     goto locout;
 
   /* Create a null-terminated and whitespace-stripped copy of Location: */
-  copyloc = malloc(llen + 1);
+  copyloc = memdup0(loc, llen);
   if(!copyloc)
     goto locout;
-  memcpy(copyloc, loc, llen);
-  copyloc[llen] = 0;
 
   /* The original URL to use as a base for a relative redirect URL */
   if(curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &locurl))
index a5725d6d406524adc12cefc296b9043e1cbfbc86..ff80b56b92635b43175cd8526d68492421611f0c 100644 (file)
@@ -22,6 +22,7 @@
  *
  ***************************************************************************/
 #include "tool_strdup.h"
+#include "memdebug.h" /* keep this as LAST include */
 
 #ifndef HAVE_STRDUP
 char *strdup(const char *str)
@@ -42,3 +43,14 @@ char *strdup(const char *str)
   return newstr;
 }
 #endif
+
+char *memdup0(const char *data, size_t len)
+{
+  char *p = malloc(len + 1);
+  if(!p)
+    return NULL;
+  if(len)
+    memcpy(p, data, len);
+  p[len] = 0;
+  return p;
+}
index 9b216189627f7d6a4f8df3b415b9a5c5265d0651..275be7c5d907508207c12526dd1b7ba5f7f3f6c7 100644 (file)
@@ -28,5 +28,6 @@
 #ifndef HAVE_STRDUP
 extern char *strdup(const char *str);
 #endif
+char *memdup0(const char *data, size_t len);
 
 #endif /* HEADER_TOOL_STRDUP_H */
index c20bbd4ea3e4f8babb982886ac379982c2cbfc1b..d79aa2990b4a02ea35670cbc51df6ca40560bf41 100644 (file)
@@ -27,6 +27,7 @@
 #include "tool_doswin.h"
 #include "tool_urlglob.h"
 #include "tool_vms.h"
+#include "tool_strdup.h"
 #include "memdebug.h" /* keep this as LAST include */
 
 #define GLOBERROR(string, column, code) \
@@ -45,13 +46,10 @@ static CURLcode glob_fixed(struct URLGlob *glob, char *fixed, size_t len)
   if(!pat->content.Set.elements)
     return GLOBERROR("out of memory", 0, CURLE_OUT_OF_MEMORY);
 
-  pat->content.Set.elements[0] = malloc(len + 1);
+  pat->content.Set.elements[0] = memdup0(fixed, len);
   if(!pat->content.Set.elements[0])
     return GLOBERROR("out of memory", 0, CURLE_OUT_OF_MEMORY);
 
-  memcpy(pat->content.Set.elements[0], fixed, len);
-  pat->content.Set.elements[0][len] = 0;
-
   return CURLE_OK;
 }
 
index b2fc835fbdfb790afcf9f0e26fc0557b0201923e..85d03f3ce35991f74761355f9f45bb3b874291b0 100644 (file)
--- a/src/var.c
+++ b/src/var.c
 #include "tool_parsecfg.h"
 #include "tool_paramhlp.h"
 #include "tool_writeout_json.h"
+#include "tool_strdup.h"
 #include "var.h"
 #include "memdebug.h" /* keep this as LAST include */
 
 #define MAX_EXPAND_CONTENT 10000000
 #define MAX_VAR_LEN 128 /* max length of a name */
 
-static char *Memdup(const char *data, size_t len)
-{
-  char *p = malloc(len + 1);
-  if(!p)
-    return NULL;
-  if(len)
-    memcpy(p, data, len);
-  p[len] = 0;
-  return p;
-}
-
 /* free everything */
 void varcleanup(struct GlobalConfig *global)
 {
@@ -208,7 +198,7 @@ static ParameterError varfunc(struct GlobalConfig *global,
       free(c);
 
     clen = curlx_dyn_len(out);
-    c = Memdup(curlx_dyn_ptr(out), clen);
+    c = memdup0(curlx_dyn_ptr(out), clen);
     if(!c) {
       err = PARAM_NO_MEM;
       break;
@@ -379,7 +369,7 @@ static ParameterError addvariable(struct GlobalConfig *global,
   if(p) {
     memcpy(p->name, name, nlen);
 
-    p->content = contalloc ? content : Memdup(content, clen);
+    p->content = contalloc ? content : memdup0(content, clen);
     if(p->content) {
       p->clen = clen;