cookies: create the cookiejar even if no cookies to save

Important for when the file is going to be read again and thus must not
contain old contents!

Adds test 327 to verify.

Reported-by: daboul on github
Fixes #3299
Closes #3300
This commit is contained in:
Daniel Stenberg 2018-11-22 13:24:02 +01:00
parent 2e49610853
commit a52e46f390
No known key found for this signature in database
GPG key ID: 5CC908FDB71E12C2
4 changed files with 108 additions and 37 deletions

View file

@ -1456,22 +1456,9 @@ static int cookie_output(struct CookieInfo *c, const char *dumphere)
unsigned int j;
struct Cookie **array;
if((NULL == c) || (0 == c->numcookies))
/* If there are no known cookies, we don't write or even create any
destination file */
return 0;
/* at first, remove expired cookies */
remove_expired(c);
/* make sure we still have cookies after expiration */
if(0 == c->numcookies)
return 0;
array = malloc(sizeof(struct Cookie *) * c->numcookies);
if(!array)
return 1;
if(!strcmp("-", dumphere)) {
/* use stdout */
out = stdout;
@ -1480,7 +1467,6 @@ static int cookie_output(struct CookieInfo *c, const char *dumphere)
else {
out = fopen(dumphere, FOPEN_WRITETEXT);
if(!out) {
free(array);
return 1; /* failure */
}
}
@ -1490,32 +1476,40 @@ static int cookie_output(struct CookieInfo *c, const char *dumphere)
"# This file was generated by libcurl! Edit at your own risk.\n\n",
out);
j = 0;
for(i = 0; i < COOKIE_HASH_SIZE; i++) {
for(co = c->cookies[i]; co; co = co->next) {
if(!co->domain)
continue;
array[j++] = co;
}
}
qsort(array, c->numcookies, sizeof(struct Cookie *), cookie_sort_ct);
for(i = 0; i < j; i++) {
format_ptr = get_netscape_format(array[i]);
if(format_ptr == NULL) {
fprintf(out, "#\n# Fatal libcurl error\n");
free(array);
if(c->numcookies) {
array = malloc(sizeof(struct Cookie *) * c->numcookies);
if(!array) {
if(!use_stdout)
fclose(out);
return 1;
}
fprintf(out, "%s\n", format_ptr);
free(format_ptr);
j = 0;
for(i = 0; i < COOKIE_HASH_SIZE; i++) {
for(co = c->cookies[i]; co; co = co->next) {
if(!co->domain)
continue;
array[j++] = co;
}
}
qsort(array, c->numcookies, sizeof(struct Cookie *), cookie_sort_ct);
for(i = 0; i < j; i++) {
format_ptr = get_netscape_format(array[i]);
if(format_ptr == NULL) {
fprintf(out, "#\n# Fatal libcurl error\n");
free(array);
if(!use_stdout)
fclose(out);
return 1;
}
fprintf(out, "%s\n", format_ptr);
free(format_ptr);
}
free(array);
}
free(array);
if(!use_stdout)
fclose(out);