From 1dba44b2f1d8b813526610044aef5adbb9419dac Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Mon, 8 Jan 2024 17:00:05 +0100 Subject: [PATCH] tool_getparam: replace malloc + copy by dynbuf for --data --- src/tool_getparam.c | 42 ++++++++++++++++-------------------------- src/tool_operate.c | 1 - src/tool_paramhlp.c | 2 -- src/tool_paramhlp.h | 2 ++ 4 files changed, 18 insertions(+), 29 deletions(-) diff --git a/src/tool_getparam.c b/src/tool_getparam.c index ddc8ff7b33..6a7402b168 100644 --- a/src/tool_getparam.c +++ b/src/tool_getparam.c @@ -860,34 +860,24 @@ static ParameterError set_data(char subletter, config->jsoned = TRUE; if(config->postfields) { - /* we already have a string, we append this one with a separating - &-letter */ - char *oldpost = config->postfields; - curl_off_t oldlen = config->postfieldsize; - curl_off_t newlen = oldlen + curlx_uztoso(size) + 2; - config->postfields = malloc((size_t)newlen); - if(!config->postfields) { - Curl_safefree(oldpost); - Curl_safefree(postdata); + /* we already have a string, append this one - perhaps with a separator */ + struct curlx_dynbuf out; + curlx_dyn_init(&out, MAX_FILE2MEMORY); + if(curlx_dyn_addn(&out, config->postfields, + (size_t)config->postfieldsize)) err = PARAM_NO_MEM; - goto done; - } - memcpy(config->postfields, oldpost, (size_t)oldlen); - if(subletter != 'f') { - /* skip this treatment for --json */ - /* use byte value 0x26 for '&' to accommodate non-ASCII platforms */ - config->postfields[oldlen] = '\x26'; - memcpy(&config->postfields[oldlen + 1], postdata, size); - config->postfields[oldlen + 1 + size] = '\0'; - config->postfieldsize += size + 1; - } - else { - memcpy(&config->postfields[oldlen], postdata, size); - config->postfields[oldlen + size] = '\0'; - config->postfieldsize += size; - } - Curl_safefree(oldpost); + + /* skip the separator append for --json */ + if(!err && (subletter != 'f') && curlx_dyn_addn(&out, "&", 1)) + err = PARAM_NO_MEM; + + if(!err && curlx_dyn_addn(&out, postdata, size)) + err = PARAM_NO_MEM; + + config->postfieldsize = curlx_dyn_len(&out); + free(config->postfields); Curl_safefree(postdata); + config->postfields = curlx_dyn_ptr(&out); } else { config->postfields = postdata; diff --git a/src/tool_operate.c b/src/tool_operate.c index c805b7732b..2b0ed8620e 100644 --- a/src/tool_operate.c +++ b/src/tool_operate.c @@ -372,7 +372,6 @@ void single_transfer_cleanup(struct OperationConfig *config) state->urls = NULL; } Curl_safefree(state->outfiles); - Curl_safefree(state->httpgetfields); Curl_safefree(state->uploadfile); if(state->inglob) { /* Free list of globbed upload files */ diff --git a/src/tool_paramhlp.c b/src/tool_paramhlp.c index a6b946edd6..2725815000 100644 --- a/src/tool_paramhlp.c +++ b/src/tool_paramhlp.c @@ -88,8 +88,6 @@ ParameterError file2string(char **bufp, FILE *file) return PARAM_OK; } -#define MAX_FILE2MEMORY (1024*1024*1024) /* big enough ? */ - ParameterError file2memory(char **bufp, size_t *size, FILE *file) { if(file) { diff --git a/src/tool_paramhlp.h b/src/tool_paramhlp.h index edb8781950..96c49ac59e 100644 --- a/src/tool_paramhlp.h +++ b/src/tool_paramhlp.h @@ -30,6 +30,8 @@ struct getout *new_getout(struct OperationConfig *config); ParameterError file2string(char **bufp, FILE *file); +#define MAX_FILE2MEMORY (1024*1024*1024) /* big enough ? */ + ParameterError file2memory(char **bufp, size_t *size, FILE *file); ParameterError str2num(long *val, const char *str); -- 2.47.3