]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
formdata: fix memory leak in OOM situation
authorDaniel Stenberg <daniel@haxx.se>
Tue, 20 May 2025 07:05:16 +0000 (09:05 +0200)
committerDaniel Stenberg <daniel@haxx.se>
Tue, 20 May 2025 08:44:53 +0000 (10:44 +0200)
Fixes #17390
Follow-up to c26da713e7dfa8c26a71bf3c0c75
Reported-by: Viktor Szakats
Closes #17393

lib/formdata.c

index d6b55b76833f7f12e5af8566f45c81ca0cba8a0f..2aa8eee94aa8afb13f97ecfdf574410f44230897 100644 (file)
@@ -310,6 +310,19 @@ static CURLFORMcode FormAddCheck(struct FormInfo *first_form,
   return CURL_FORMADD_OK;
 }
 
+/* Shallow cleanup. Remove the newly created chain, the structs only and not
+   the content they point to */
+static void free_chain(struct curl_httppost *c)
+{
+  while(c) {
+    struct curl_httppost *next = c->next;
+    if(c->more)
+      free_chain(c->more);
+    free(c);
+    c = next;
+  }
+}
+
 static
 CURLFORMcode FormAdd(struct curl_httppost **httppost,
                      struct curl_httppost **last_post,
@@ -648,14 +661,8 @@ CURLFORMcode FormAdd(struct curl_httppost **httppost,
 
     (*last_post) = lastnode;
   }
-  else {
-    /* remove the newly created chain */
-    while(newchain) {
-      struct curl_httppost *next = newchain->next;
-      free(newchain);
-      newchain = next;
-    }
-  }
+  else
+    free_chain(newchain);
 
   return retval;
 }