cookie: flush better

The cookie flushing (saving to a cookie jar) should only be done if a
transfer has been started. This is now done by checking the
cookies->running field, which is not reset in curl_easy_reset() so the
saving works correctly even after a call to that.

Follow-up to fd6eb8d6e7

Verified by test 1920

Reported-by: Alexander Batischev
Fixes #20090
Closes #20094
This commit is contained in:
Daniel Stenberg 2025-12-25 10:53:11 +01:00
parent e78a466ebd
commit 18af4e4e10
No known key found for this signature in database
GPG key ID: 5CC908FDB71E12C2
5 changed files with 138 additions and 16 deletions

View file

@ -1607,22 +1607,24 @@ struct curl_slist *Curl_cookie_list(struct Curl_easy *data)
void Curl_flush_cookies(struct Curl_easy *data, bool cleanup)
{
Curl_share_lock(data, CURL_LOCK_DATA_COOKIE, CURL_LOCK_ACCESS_SINGLE);
/* only save the cookie file if a transfer was started (data->state.url is
/* only save the cookie file if a transfer was started (cookies->running is
set), as otherwise the cookies were not completely initialized and there
might be cookie files that were not loaded so saving the file is the wrong
thing. */
if(data->set.str[STRING_COOKIEJAR] && Curl_bufref_ptr(&data->state.url)) {
/* if we have a destination file for all the cookies to get dumped to */
CURLcode result = cookie_output(data, data->cookies,
data->set.str[STRING_COOKIEJAR]);
if(result)
infof(data, "WARNING: failed to save cookies in %s: %s",
data->set.str[STRING_COOKIEJAR], curl_easy_strerror(result));
}
might be cookie files that were not loaded so saving the file is the
wrong thing. */
if(data->cookies) {
if(data->set.str[STRING_COOKIEJAR] && data->cookies->running) {
/* if we have a destination file for all the cookies to get dumped to */
CURLcode result = cookie_output(data, data->cookies,
data->set.str[STRING_COOKIEJAR]);
if(result)
infof(data, "WARNING: failed to save cookies in %s: %s",
data->set.str[STRING_COOKIEJAR], curl_easy_strerror(result));
}
if(cleanup && (!data->share || (data->cookies != data->share->cookies))) {
Curl_cookie_cleanup(data->cookies);
data->cookies = NULL;
if(cleanup && (!data->share || (data->cookies != data->share->cookies))) {
Curl_cookie_cleanup(data->cookies);
data->cookies = NULL;
}
}
Curl_share_unlock(data, CURL_LOCK_DATA_COOKIE);
}