1
0
mirror of https://github.com/moparisthebest/curl synced 2024-11-05 00:55:04 -05:00

cookies: use CURLcode for cookie_output reporting

Writing the cookie file has multiple error conditions, and was using an
int with magic numbers to report the different error (which in turn were
disregarded anyways). This moves reporting to use a CURLcode value.

Lightly-touched-by: Daniel Stenberg

Closes #7037
Closes #6749
This commit is contained in:
Daniel Gustafsson 2021-03-15 03:08:31 +01:00 committed by Daniel Stenberg
parent 98888e6070
commit 24c71d62ee
No known key found for this signature in database
GPG Key ID: 5CC908FDB71E12C2

View File

@ -1571,18 +1571,18 @@ static char *get_netscape_format(const struct Cookie *co)
*
* The function returns non-zero on write failure.
*/
static int cookie_output(struct Curl_easy *data,
struct CookieInfo *c, const char *filename)
static CURLcode cookie_output(struct Curl_easy *data,
struct CookieInfo *c, const char *filename)
{
struct Cookie *co;
FILE *out = NULL;
bool use_stdout = FALSE;
char *tempstore = NULL;
bool error = false;
CURLcode error = CURLE_OK;
if(!c)
/* no cookie engine alive */
return 0;
return CURLE_OK;
/* at first, remove expired cookies */
remove_expired(c);
@ -1600,11 +1600,13 @@ static int cookie_output(struct Curl_easy *data,
tempstore = aprintf("%s.%s.tmp", filename, randsuffix);
if(!tempstore)
return 1;
return CURLE_OUT_OF_MEMORY;
out = fopen(tempstore, FOPEN_WRITETEXT);
if(!out)
if(!out) {
error = CURLE_WRITE_ERROR;
goto error;
}
}
fputs("# Netscape HTTP Cookie File\n"
@ -1619,6 +1621,7 @@ static int cookie_output(struct Curl_easy *data,
array = calloc(1, sizeof(struct Cookie *) * c->numcookies);
if(!array) {
error = CURLE_OUT_OF_MEMORY;
goto error;
}
@ -1636,8 +1639,8 @@ static int cookie_output(struct Curl_easy *data,
for(i = 0; i < nvalid; i++) {
char *format_ptr = get_netscape_format(array[i]);
if(!format_ptr) {
fprintf(out, "#\n# Fatal libcurl error\n");
free(array);
error = CURLE_OUT_OF_MEMORY;
goto error;
}
fprintf(out, "%s\n", format_ptr);
@ -1652,18 +1655,24 @@ static int cookie_output(struct Curl_easy *data,
out = NULL;
if(Curl_rename(tempstore, filename)) {
unlink(tempstore);
error = CURLE_WRITE_ERROR;
goto error;
}
}
goto cleanup;
/*
* If we reach here we have successfully written a cookie file so theree is
* no need to inspect the error, any error case should have jumped into the
* error block below.
*/
free(tempstore);
return CURLE_OK;
error:
error = true;
cleanup:
if(out && !use_stdout)
fclose(out);
free(tempstore);
return error ? 1 : 0;
return error;
}
static struct curl_slist *cookie_list(struct Curl_easy *data)
@ -1710,6 +1719,8 @@ struct curl_slist *Curl_cookie_list(struct Curl_easy *data)
void Curl_flush_cookies(struct Curl_easy *data, bool cleanup)
{
CURLcode res;
if(data->set.str[STRING_COOKIEJAR]) {
if(data->state.cookielist) {
/* If there is a list of cookie files to read, do it first so that
@ -1721,9 +1732,10 @@ void Curl_flush_cookies(struct Curl_easy *data, bool cleanup)
Curl_share_lock(data, CURL_LOCK_DATA_COOKIE, CURL_LOCK_ACCESS_SINGLE);
/* if we have a destination file for all the cookies to get dumped to */
if(cookie_output(data, data->cookies, data->set.str[STRING_COOKIEJAR]))
infof(data, "WARNING: failed to save cookies in %s\n",
data->set.str[STRING_COOKIEJAR]);
res = cookie_output(data, data->cookies, data->set.str[STRING_COOKIEJAR]);
if(res)
infof(data, "WARNING: failed to save cookies in %s: %s\n",
data->set.str[STRING_COOKIEJAR], curl_easy_strerror(res));
}
else {
if(cleanup && data->state.cookielist) {