From a6cdfd24eed5778da4d080e7c139a00ba9282ca0 Mon Sep 17 00:00:00 2001 From: Daniel Gustafsson Date: Wed, 20 Apr 2022 14:17:29 +0200 Subject: [PATCH] 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 Reviewed-by: Jay Satiro --- lib/cookie.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/cookie.c b/lib/cookie.c index d418efa33d..859129d3e3 100644 --- a/lib/cookie.c +++ b/lib/cookie.c @@ -1188,12 +1188,15 @@ struct CookieInfo *Curl_cookie_init(struct Curl_easy *data, fp = stdin; fromfile = FALSE; } - else if(file && !*file) { - /* points to a "" string */ + else if(!file || !*file) { + /* points to an empty string or NULL */ fp = NULL; } - else - fp = file?fopen(file, FOPEN_READTEXT):NULL; + else { + fp = fopen(file, FOPEN_READTEXT); + if(!fp) + infof(data, "WARNING: failed to open cookie file \"%s\"", file); + } c->newsession = newsession; /* new session? */ @@ -1227,7 +1230,7 @@ struct CookieInfo *Curl_cookie_init(struct Curl_easy *data, */ remove_expired(c); - if(fromfile) + if(fromfile && fp) fclose(fp); } -- 2.47.3