cookies: Improve errorhandling for reading cookiefile

The existing programming had some issues with errorhandling for reading
the cookie file. If the file failed to open, we would silently ignore it
and continue as if there was no file (or stdin) passed. In this case, we
would also call fclose() on the NULL FILE pointer, which is undefined
behavior. Fix by ensuring that the FILE pointer is set before calling
fclose on it, and issue a warning in case the file cannot be opened.
Erroring out on nonexisting file would break backwards compatibility of
very old behavior so we can't really go there.

Closes: #8699
Reviewed-by: Daniel Stenberg <daniel@haxx.se>
Reviewed-by: Jay Satiro <raysatiro@yahoo.com>
This commit is contained in:
Daniel Gustafsson 2022-04-20 14:17:29 +02:00
Родитель d794d4bce2
Коммит a6cdfd24ee
1 изменённых файлов: 8 добавлений и 5 удалений

Просмотреть файл

@ -1188,12 +1188,15 @@ struct CookieInfo *Curl_cookie_init(struct Curl_easy *data,
fp = stdin; fp = stdin;
fromfile = FALSE; fromfile = FALSE;
} }
else if(file && !*file) { else if(!file || !*file) {
/* points to a "" string */ /* points to an empty string or NULL */
fp = NULL; fp = NULL;
} }
else else {
fp = file?fopen(file, FOPEN_READTEXT):NULL; fp = fopen(file, FOPEN_READTEXT);
if(!fp)
infof(data, "WARNING: failed to open cookie file \"%s\"", file);
}
c->newsession = newsession; /* new session? */ c->newsession = newsession; /* new session? */
@ -1227,7 +1230,7 @@ struct CookieInfo *Curl_cookie_init(struct Curl_easy *data,
*/ */
remove_expired(c); remove_expired(c);
if(fromfile) if(fromfile && fp)
fclose(fp); fclose(fp);
} }