]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
tool_getparam: parse_header() without strtok
authorDaniel Stenberg <daniel@haxx.se>
Wed, 5 Mar 2025 12:43:48 +0000 (13:43 +0100)
committerDaniel Stenberg <daniel@haxx.se>
Wed, 5 Mar 2025 13:55:55 +0000 (14:55 +0100)
Read a provided header file line-by-line instead. Avoids strtok(),
avoids a possibly quite large malloc for the entire file.

Closes #16572

src/tool_getparam.c

index 3a537fb5f247c73a204ec6650bcc97d594b35bed..83e87d2393b4e5e49123ec1b24285f745834de97 100644 (file)
@@ -1284,8 +1284,6 @@ static ParameterError parse_header(struct GlobalConfig *global,
   /* A custom header to append to a list */
   if(nextarg[0] == '@') {
     /* read many headers from a file or stdin */
-    char *string;
-    size_t len;
     bool use_stdin = !strcmp(&nextarg[1], "-");
     FILE *file = use_stdin ? stdin : fopen(&nextarg[1], FOPEN_READTEXT);
     if(!file) {
@@ -1293,22 +1291,26 @@ static ParameterError parse_header(struct GlobalConfig *global,
       err = PARAM_READ_ERROR;
     }
     else {
-      err = file2memory(&string, &len, file);
-      if(!err && string) {
-        /* Allow strtok() here since this is not used threaded */
-        /* !checksrc! disable BANNEDFUNC 2 */
-        char *h = strtok(string, "\r\n");
-        while(h) {
-          if(cmd == C_PROXY_HEADER) /* --proxy-header */
-            err = add2list(&config->proxyheaders, h);
-          else
-            err = add2list(&config->headers, h);
+      struct dynbuf line;
+      bool error = FALSE;
+      curlx_dyn_init(&line, 1024*100);
+      while(my_get_line(file, &line, &error)) {
+        /* every line has a newline that we strip off */
+        size_t len = curlx_dyn_len(&line);
+        if(len) {
+          const char *ptr;
+          curlx_dyn_setlen(&line, len - 1);
+          ptr = curlx_dyn_ptr(&line);
+          err = add2list(cmd == C_PROXY_HEADER ? /* --proxy-header? */
+                         &config->proxyheaders :
+                         &config->headers, ptr);
           if(err)
             break;
-          h = strtok(NULL, "\r\n");
         }
-        free(string);
       }
+      if(error)
+        err = PARAM_READ_ERROR;
+      curlx_dyn_free(&line);
       if(!use_stdin)
         fclose(file);
     }
@@ -1415,7 +1417,7 @@ static ParameterError parse_quote(struct OperationConfig *config,
     /* prefixed with a plus makes it a just-before-transfer one */
     nextarg++;
     err = add2list(&config->prequote, nextarg);
-        break;
+    break;
   default:
     err = add2list(&config->quote, nextarg);
     break;