tool_operhlp: fix add_file_name_to_url() result on OOM

Return `CURLE_OUT_OF_MEMORY` instead of `CURLE_URL_MALFORMAT` when
`curl_url()`, `curl_easy_escape()`, or `curl_maprintf()` calls failed.

Found by Codex Security

Also reuse deinit code from a success branch.

Closes #21011
This commit is contained in:
Viktor Szakats 2026-03-19 15:47:05 +01:00
parent b9e179e4e5
commit 07c10f09a5
No known key found for this signature in database

View file

@ -83,7 +83,7 @@ CURLcode urlerr_cvt(CURLUcode ucode)
*/
CURLcode add_file_name_to_url(CURL *curl, char **inurlp, const char *filename)
{
CURLcode result = CURLE_URL_MALFORMAT;
CURLcode result = CURLE_OUT_OF_MEMORY;
CURLUcode uerr;
CURLU *uh = curl_url();
char *path = NULL;
@ -94,19 +94,18 @@ CURLcode add_file_name_to_url(CURL *curl, char **inurlp, const char *filename)
CURLU_GUESS_SCHEME | CURLU_NON_SUPPORT_SCHEME);
if(uerr) {
result = urlerr_cvt(uerr);
goto fail;
goto out;
}
uerr = curl_url_get(uh, CURLUPART_PATH, &path, 0);
if(uerr) {
result = urlerr_cvt(uerr);
goto fail;
goto out;
}
uerr = curl_url_get(uh, CURLUPART_QUERY, &query, 0);
if(!uerr && query) {
curl_free(query);
curl_free(path);
curl_url_cleanup(uh);
return CURLE_OK;
result = CURLE_OK;
goto out;
}
ptr = strrchr(path, '/');
if(!ptr || !*++ptr) {
@ -141,17 +140,17 @@ CURLcode add_file_name_to_url(CURL *curl, char **inurlp, const char *filename)
curl_free(encfile);
if(!newpath)
goto fail;
goto out;
uerr = curl_url_set(uh, CURLUPART_PATH, newpath, 0);
curlx_free(newpath);
if(uerr) {
result = urlerr_cvt(uerr);
goto fail;
goto out;
}
uerr = curl_url_get(uh, CURLUPART_URL, &newurl, CURLU_DEFAULT_SCHEME);
if(uerr) {
result = urlerr_cvt(uerr);
goto fail;
goto out;
}
curlx_free(*inurlp);
*inurlp = newurl;
@ -159,10 +158,9 @@ CURLcode add_file_name_to_url(CURL *curl, char **inurlp, const char *filename)
}
}
else
/* nothing to do */
result = CURLE_OK;
result = CURLE_OK; /* nothing to do */
}
fail:
out:
curl_url_cleanup(uh);
curl_free(path);
return result;