tool_getparam: make data_urlencode avoid direct malloc

use aprintf() instead
This commit is contained in:
Daniel Stenberg 2024-01-08 17:00:05 +01:00
parent 3870d03b5d
commit f37840a46e
No known key found for this signature in database
GPG key ID: 5CC908FDB71E12C2

View file

@ -637,25 +637,18 @@ static ParameterError data_urlencode(struct GlobalConfig *global,
char *enc = curl_easy_escape(NULL, postdata, (int)size); char *enc = curl_easy_escape(NULL, postdata, (int)size);
Curl_safefree(postdata); /* no matter if it worked or not */ Curl_safefree(postdata); /* no matter if it worked or not */
if(enc) { if(enc) {
/* replace (in-place) '%20' by '+' according to RFC1866 */ char *n;
size_t enclen = replace_url_encoded_space_by_plus(enc); replace_url_encoded_space_by_plus(enc);
/* now make a string with the name from above and append the
encoded string */
size_t outlen = nlen + enclen + 2;
char *n = malloc(outlen);
if(!n) {
curl_free(enc);
return PARAM_NO_MEM;
}
if(nlen > 0) { /* only append '=' if we have a name */ if(nlen > 0) { /* only append '=' if we have a name */
msnprintf(n, outlen, "%.*s=%s", (int)nlen, nextarg, enc); n = aprintf("%.*s=%s", (int)nlen, nextarg, enc);
size = outlen-1; curl_free(enc);
if(!n)
return PARAM_NO_MEM;
} }
else { else
strcpy(n, enc); n = enc;
size = outlen-2; /* since no '=' was inserted */
} size = strlen(n);
curl_free(enc);
postdata = n; postdata = n;
} }
else else