cookie: clarify that init with data set to NULL reads no file
... and make Curl_cookie_add() require 'data' being set proper with an assert. The function has not worked with a NULL data for quite some time so this just corrects the code and comment. This is a different take than the proposed fixed in #10927 Reported-by: Kvarec Lezki Ref: #10929 Closes #10930
This commit is contained in:
parent
a1763648a5
commit
b1b326ec50
48
lib/cookie.c
48
lib/cookie.c
@ -483,11 +483,6 @@ static int invalid_octets(const char *p)
|
|||||||
*/
|
*/
|
||||||
struct Cookie *
|
struct Cookie *
|
||||||
Curl_cookie_add(struct Curl_easy *data,
|
Curl_cookie_add(struct Curl_easy *data,
|
||||||
/*
|
|
||||||
* The 'data' pointer here may be NULL at times, and thus
|
|
||||||
* must only be used very carefully for things that can deal
|
|
||||||
* with data being NULL. Such as infof() and similar
|
|
||||||
*/
|
|
||||||
struct CookieInfo *c,
|
struct CookieInfo *c,
|
||||||
bool httpheader, /* TRUE if HTTP header-style line */
|
bool httpheader, /* TRUE if HTTP header-style line */
|
||||||
bool noexpire, /* if TRUE, skip remove_expired() */
|
bool noexpire, /* if TRUE, skip remove_expired() */
|
||||||
@ -508,10 +503,7 @@ Curl_cookie_add(struct Curl_easy *data,
|
|||||||
bool badcookie = FALSE; /* cookies are good by default. mmmmm yummy */
|
bool badcookie = FALSE; /* cookies are good by default. mmmmm yummy */
|
||||||
size_t myhash;
|
size_t myhash;
|
||||||
|
|
||||||
#ifdef CURL_DISABLE_VERBOSE_STRINGS
|
DEBUGASSERT(data);
|
||||||
(void)data;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
DEBUGASSERT(MAX_SET_COOKIE_AMOUNT <= 255); /* counter is an unsigned char */
|
DEBUGASSERT(MAX_SET_COOKIE_AMOUNT <= 255); /* counter is an unsigned char */
|
||||||
if(data->req.setcookies >= MAX_SET_COOKIE_AMOUNT)
|
if(data->req.setcookies >= MAX_SET_COOKIE_AMOUNT)
|
||||||
return NULL;
|
return NULL;
|
||||||
@ -1219,7 +1211,8 @@ Curl_cookie_add(struct Curl_easy *data,
|
|||||||
*
|
*
|
||||||
* If 'newsession' is TRUE, discard all "session cookies" on read from file.
|
* If 'newsession' is TRUE, discard all "session cookies" on read from file.
|
||||||
*
|
*
|
||||||
* Note that 'data' might be called as NULL pointer.
|
* Note that 'data' might be called as NULL pointer. If data is NULL, 'file'
|
||||||
|
* will be ignored.
|
||||||
*
|
*
|
||||||
* Returns NULL on out of memory. Invalid cookies are ignored.
|
* Returns NULL on out of memory. Invalid cookies are ignored.
|
||||||
*/
|
*/
|
||||||
@ -1229,9 +1222,8 @@ struct CookieInfo *Curl_cookie_init(struct Curl_easy *data,
|
|||||||
bool newsession)
|
bool newsession)
|
||||||
{
|
{
|
||||||
struct CookieInfo *c;
|
struct CookieInfo *c;
|
||||||
FILE *fp = NULL;
|
|
||||||
bool fromfile = TRUE;
|
|
||||||
char *line = NULL;
|
char *line = NULL;
|
||||||
|
FILE *handle = NULL;
|
||||||
|
|
||||||
if(!inc) {
|
if(!inc) {
|
||||||
/* we didn't get a struct, create one */
|
/* we didn't get a struct, create one */
|
||||||
@ -1251,24 +1243,23 @@ struct CookieInfo *Curl_cookie_init(struct Curl_easy *data,
|
|||||||
/* we got an already existing one, use that */
|
/* we got an already existing one, use that */
|
||||||
c = inc;
|
c = inc;
|
||||||
}
|
}
|
||||||
c->running = FALSE; /* this is not running, this is init */
|
c->newsession = newsession; /* new session? */
|
||||||
|
|
||||||
if(file && !strcmp(file, "-")) {
|
if(data) {
|
||||||
|
FILE *fp = NULL;
|
||||||
|
if(file) {
|
||||||
|
if(!strcmp(file, "-"))
|
||||||
fp = stdin;
|
fp = stdin;
|
||||||
fromfile = FALSE;
|
|
||||||
}
|
|
||||||
else if(!file || !*file) {
|
|
||||||
/* points to an empty string or NULL */
|
|
||||||
fp = NULL;
|
|
||||||
}
|
|
||||||
else {
|
else {
|
||||||
fp = fopen(file, "rb");
|
fp = fopen(file, "rb");
|
||||||
if(!fp)
|
if(!fp)
|
||||||
infof(data, "WARNING: failed to open cookie file \"%s\"", file);
|
infof(data, "WARNING: failed to open cookie file \"%s\"", file);
|
||||||
|
else
|
||||||
|
handle = fp;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
c->newsession = newsession; /* new session? */
|
c->running = FALSE; /* this is not running, this is init */
|
||||||
|
|
||||||
if(fp) {
|
if(fp) {
|
||||||
char *lineptr;
|
char *lineptr;
|
||||||
bool headerline;
|
bool headerline;
|
||||||
@ -1299,13 +1290,12 @@ struct CookieInfo *Curl_cookie_init(struct Curl_easy *data,
|
|||||||
*/
|
*/
|
||||||
remove_expired(c);
|
remove_expired(c);
|
||||||
|
|
||||||
if(fromfile)
|
if(handle)
|
||||||
fclose(fp);
|
fclose(handle);
|
||||||
}
|
}
|
||||||
|
|
||||||
c->running = TRUE; /* now, we're running */
|
|
||||||
if(data)
|
|
||||||
data->state.cookie_engine = TRUE;
|
data->state.cookie_engine = TRUE;
|
||||||
|
c->running = TRUE; /* now, we're running */
|
||||||
|
}
|
||||||
|
|
||||||
return c;
|
return c;
|
||||||
|
|
||||||
@ -1317,8 +1307,8 @@ fail:
|
|||||||
*/
|
*/
|
||||||
if(!inc)
|
if(!inc)
|
||||||
Curl_cookie_cleanup(c);
|
Curl_cookie_cleanup(c);
|
||||||
if(fromfile && fp)
|
if(handle)
|
||||||
fclose(fp);
|
fclose(handle);
|
||||||
return NULL; /* out of memory */
|
return NULL; /* out of memory */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -61,7 +61,6 @@ struct Cookie {
|
|||||||
struct CookieInfo {
|
struct CookieInfo {
|
||||||
/* linked list of cookies we know of */
|
/* linked list of cookies we know of */
|
||||||
struct Cookie *cookies[COOKIE_HASH_SIZE];
|
struct Cookie *cookies[COOKIE_HASH_SIZE];
|
||||||
|
|
||||||
char *filename; /* file we read from/write to */
|
char *filename; /* file we read from/write to */
|
||||||
long numcookies; /* number of cookies in the "jar" */
|
long numcookies; /* number of cookies in the "jar" */
|
||||||
bool running; /* state info, for cookie adding information */
|
bool running; /* state info, for cookie adding information */
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user