]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
ftp: use dynbuf to store entrypath
authorDaniel Stenberg <daniel@haxx.se>
Fri, 5 Jan 2024 10:52:08 +0000 (11:52 +0100)
committerDaniel Stenberg <daniel@haxx.se>
Sat, 6 Jan 2024 10:25:37 +0000 (11:25 +0100)
avoid direct malloc

Closes #12638

lib/ftp.c

index ee3b403d3cd5f7f69b9cc157bea29ae9a42bd3e8..65b4db98154acc0d7f929fe3bab06bd927403907 100644 (file)
--- a/lib/ftp.c
+++ b/lib/ftp.c
@@ -2863,12 +2863,9 @@ static CURLcode ftp_statemachine(struct Curl_easy *data,
       if(ftpcode == 257) {
         char *ptr = &data->state.buffer[4];  /* start on the first letter */
         const size_t buf_size = data->set.buffer_size;
-        char *dir;
         bool entry_extracted = FALSE;
-
-        dir = malloc(nread + 1);
-        if(!dir)
-          return CURLE_OUT_OF_MEMORY;
+        struct dynbuf out;
+        Curl_dyn_init(&out, 1000);
 
         /* Reply format is like
            257<space>[rubbish]"<directory-name>"<space><commentary> and the
@@ -2886,13 +2883,11 @@ static CURLcode ftp_statemachine(struct Curl_easy *data,
 
         if('\"' == *ptr) {
           /* it started good */
-          char *store;
-          ptr++;
-          for(store = dir; *ptr;) {
+          for(ptr++; *ptr; ptr++) {
             if('\"' == *ptr) {
               if('\"' == ptr[1]) {
                 /* "quote-doubling" */
-                *store = ptr[1];
+                result = Curl_dyn_addn(&out, &ptr[1], 1);
                 ptr++;
               }
               else {
@@ -2902,11 +2897,10 @@ static CURLcode ftp_statemachine(struct Curl_easy *data,
               }
             }
             else
-              *store = *ptr;
-            store++;
-            ptr++;
+              result = Curl_dyn_addn(&out, ptr, 1);
+            if(result)
+              return result;
           }
-          *store = '\0'; /* null-terminate */
         }
         if(entry_extracted) {
           /* If the path name does not look like an absolute path (i.e.: it
@@ -2920,6 +2914,7 @@ static CURLcode ftp_statemachine(struct Curl_easy *data,
                The method used here is to check the server OS: we do it only
              if the path name looks strange to minimize overhead on other
              systems. */
+          char *dir = Curl_dyn_ptr(&out);
 
           if(!ftpc->server_os && dir[0] != '/') {
             result = Curl_pp_sendf(data, &ftpc->pp, "%s", "SYST");
@@ -2944,7 +2939,7 @@ static CURLcode ftp_statemachine(struct Curl_easy *data,
         }
         else {
           /* couldn't get the path */
-          free(dir);
+          Curl_dyn_free(&out);
           infof(data, "Failed to figure out path");
         }
       }