From 03b547f73f8dcfddb108bbfdbdc979f0b55595f1 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Tue, 5 May 2026 09:20:47 +0200 Subject: [PATCH] tool_formparse.c: use define instead of magic number The longest header lines accepted for the -F option is now a define instead of a magic number. I also bumped it to be an even 8K. When fixing, I noticed that for some OOM errors curl would display two error messages. Also fixed here. Closes #21501 --- src/tool_formparse.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/tool_formparse.c b/src/tool_formparse.c index b8161813b0..4db2dce96b 100644 --- a/src/tool_formparse.c +++ b/src/tool_formparse.c @@ -408,14 +408,17 @@ static int slist_append(struct curl_slist **plist, const char *data) return 0; } -/* Read headers from a file and append to list. */ +#define HEADER_LINE_BUFFER_SIZE 8192 + +/* Read headers from a file and append to list. + Return zero on success, non-zero on error. */ static int read_field_headers(FILE *fp, struct curl_slist **pheaders) { struct dynbuf line; bool error = FALSE; int err = 0; - curlx_dyn_init(&line, 8092); + curlx_dyn_init(&line, HEADER_LINE_BUFFER_SIZE); while(my_get_line(fp, &line, &error)) { const char *ptr = curlx_dyn_ptr(&line); size_t len = curlx_dyn_len(&line); @@ -436,7 +439,7 @@ static int read_field_headers(FILE *fp, struct curl_slist **pheaders) /* append this new line onto the previous line */ struct dynbuf amend; struct curl_slist *l = *pheaders; - curlx_dyn_init(&amend, 8092); + curlx_dyn_init(&amend, HEADER_LINE_BUFFER_SIZE); /* find the last node */ while(l && l->next) l = l->next; @@ -450,14 +453,12 @@ static int read_field_headers(FILE *fp, struct curl_slist **pheaders) curl_slist_append */ l->data = curl_maprintf("%s", curlx_dyn_ptr(&amend)); curlx_dyn_free(&amend); - if(!l->data) { - errorf("Out of memory for field headers"); - err = 1; - } + if(!l->data) + err = -1; } - else { + else err = slist_append(pheaders, ptr); - } + if(err) { errorf("Out of memory for field headers"); err = -1;