1
0
mirror of https://github.com/moparisthebest/curl synced 2024-12-21 23:58:49 -05:00

general cleanup to bail out nice and clean when a memory function fails

to deliver
This commit is contained in:
Daniel Stenberg 2004-05-12 12:04:38 +00:00
parent a219d774fe
commit 34e8baab9a

View File

@ -99,9 +99,10 @@ Example set of cookies:
#include "memdebug.h" #include "memdebug.h"
#endif #endif
static void static void freecookie(struct Cookie *co)
free_cookiemess(struct Cookie *co)
{ {
if(co->expirestr)
free(co->expirestr);
if(co->domain) if(co->domain)
free(co->domain); free(co->domain);
if(co->path) if(co->path)
@ -204,6 +205,10 @@ Curl_cookie_add(struct SessionHandle *data,
if(strequal("path", name)) { if(strequal("path", name)) {
co->path=strdup(whatptr); co->path=strdup(whatptr);
if(!co->path) {
badcookie = TRUE; /* out of memory bad */
break;
}
} }
else if(strequal("domain", name)) { else if(strequal("domain", name)) {
/* note that this name may or may not have a preceeding dot, but /* note that this name may or may not have a preceeding dot, but
@ -260,7 +265,12 @@ Curl_cookie_add(struct SessionHandle *data,
const char *tailptr=whatptr; const char *tailptr=whatptr;
if(tailptr[0] == '.') if(tailptr[0] == '.')
tailptr++; tailptr++;
co->domain=strdup(tailptr); /* don't prefix w/dots internally */ co->domain=strdup(tailptr); /* don't prefix w/dots
internally */
if(!co->domain) {
badcookie = TRUE;
break;
}
co->tailmatch=TRUE; /* we always do that if the domain name was co->tailmatch=TRUE; /* we always do that if the domain name was
given */ given */
} }
@ -276,6 +286,10 @@ Curl_cookie_add(struct SessionHandle *data,
} }
else if(strequal("version", name)) { else if(strequal("version", name)) {
co->version=strdup(whatptr); co->version=strdup(whatptr);
if(!co->version) {
badcookie = TRUE;
break;
}
} }
else if(strequal("max-age", name)) { else if(strequal("max-age", name)) {
/* Defined in RFC2109: /* Defined in RFC2109:
@ -288,16 +302,28 @@ Curl_cookie_add(struct SessionHandle *data,
*/ */
co->maxage = strdup(whatptr); co->maxage = strdup(whatptr);
if(!co->maxage) {
badcookie = TRUE;
break;
}
co->expires = co->expires =
atoi((*co->maxage=='\"')?&co->maxage[1]:&co->maxage[0]) + now; atoi((*co->maxage=='\"')?&co->maxage[1]:&co->maxage[0]) + now;
} }
else if(strequal("expires", name)) { else if(strequal("expires", name)) {
co->expirestr=strdup(whatptr); co->expirestr=strdup(whatptr);
if(!co->expirestr) {
badcookie = TRUE;
break;
}
co->expires = curl_getdate(what, &now); co->expires = curl_getdate(what, &now);
} }
else if(!co->name) { else if(!co->name) {
co->name = strdup(name); co->name = strdup(name);
co->value = strdup(whatptr); co->value = strdup(whatptr);
if(!co->name || !co->value) {
badcookie = TRUE;
break;
}
} }
/* /*
else this is the second (or more) name we don't know else this is the second (or more) name we don't know
@ -334,28 +360,17 @@ Curl_cookie_add(struct SessionHandle *data,
semiptr=strchr(ptr, '\0'); semiptr=strchr(ptr, '\0');
} while(semiptr); } while(semiptr);
if(badcookie || (NULL == co->name)) { if(!badcookie && !co->domain) {
/* we didn't get a cookie name or a bad one, if(domain) {
this is an illegal line, bail out */ /* no domain was given in the header line, set the default */
if(co->expirestr) co->domain=strdup(domain);
free(co->expirestr); if(!co->domain)
if(co->domain) badcookie = TRUE;
free(co->domain); }
if(co->path)
free(co->path);
if(co->name)
free(co->name);
if(co->value)
free(co->value);
free(co);
return NULL;
} }
if(NULL == co->domain) if(!badcookie && !co->path && path) {
/* no domain was given in the header line, set the default now */ /* no path was given in the header line, set the default */
co->domain=domain?strdup(domain):NULL;
if((NULL == co->path) && path) {
/* no path was given in the header line, set the default now */
char *endslash = strrchr(path, '/'); char *endslash = strrchr(path, '/');
if(endslash) { if(endslash) {
size_t pathlen = endslash-path+1; /* include the ending slash */ size_t pathlen = endslash-path+1; /* include the ending slash */
@ -364,8 +379,18 @@ Curl_cookie_add(struct SessionHandle *data,
memcpy(co->path, path, pathlen); memcpy(co->path, path, pathlen);
co->path[pathlen]=0; /* zero terminate */ co->path[pathlen]=0; /* zero terminate */
} }
else
badcookie = TRUE;
} }
} }
if(badcookie || !co->name) {
/* we didn't get a cookie name or a bad one,
this is an illegal line, bail out */
freecookie(co);
return NULL;
}
} }
else { else {
/* This line is NOT a HTTP header style line, we do offer support for /* This line is NOT a HTTP header style line, we do offer support for
@ -387,7 +412,7 @@ Curl_cookie_add(struct SessionHandle *data,
if(ptr) if(ptr)
*ptr=0; /* clear it */ *ptr=0; /* clear it */
firstptr=strtok_r(lineptr, "\t", &tok_buf); /* first tokenize it on the TAB */ firstptr=strtok_r(lineptr, "\t", &tok_buf); /* tokenize it on the TAB */
/* Here's a quick check to eliminate normal HTTP-headers from this */ /* Here's a quick check to eliminate normal HTTP-headers from this */
if(!firstptr || strchr(firstptr, ':')) { if(!firstptr || strchr(firstptr, ':')) {
@ -397,13 +422,15 @@ Curl_cookie_add(struct SessionHandle *data,
/* Now loop through the fields and init the struct we already have /* Now loop through the fields and init the struct we already have
allocated */ allocated */
for(ptr=firstptr, fields=0; ptr; for(ptr=firstptr, fields=0; ptr && !badcookie;
ptr=strtok_r(NULL, "\t", &tok_buf), fields++) { ptr=strtok_r(NULL, "\t", &tok_buf), fields++) {
switch(fields) { switch(fields) {
case 0: case 0:
if(ptr[0]=='.') /* skip preceeding dots */ if(ptr[0]=='.') /* skip preceeding dots */
ptr++; ptr++;
co->domain = strdup(ptr); co->domain = strdup(ptr);
if(!co->domain)
badcookie = TRUE;
break; break;
case 1: case 1:
/* This field got its explanation on the 23rd of May 2001 by /* This field got its explanation on the 23rd of May 2001 by
@ -425,10 +452,14 @@ Curl_cookie_add(struct SessionHandle *data,
if (strcmp("TRUE", ptr) && strcmp("FALSE", ptr)) { if (strcmp("TRUE", ptr) && strcmp("FALSE", ptr)) {
/* only if the path doesn't look like a boolean option! */ /* only if the path doesn't look like a boolean option! */
co->path = strdup(ptr); co->path = strdup(ptr);
if(!co->path)
badcookie = TRUE;
break; break;
} }
/* this doesn't look like a path, make one up! */ /* this doesn't look like a path, make one up! */
co->path = strdup("/"); co->path = strdup("/");
if(!co->path)
badcookie = TRUE;
fields++; /* add a field and fall down to secure */ fields++; /* add a field and fall down to secure */
/* FALLTHROUGH */ /* FALLTHROUGH */
case 3: case 3:
@ -439,29 +470,40 @@ Curl_cookie_add(struct SessionHandle *data,
break; break;
case 5: case 5:
co->name = strdup(ptr); co->name = strdup(ptr);
if(!co->name)
badcookie = TRUE;
break; break;
case 6: case 6:
co->value = strdup(ptr); co->value = strdup(ptr);
if(!co->value)
badcookie = TRUE;
break; break;
} }
} }
if(6 == fields) { if(6 == fields) {
/* we got a cookie with blank contents, fix it */ /* we got a cookie with blank contents, fix it */
co->value = strdup(""); co->value = strdup("");
if(!co->value)
badcookie = TRUE;
else
fields++;
} }
else if(7 != fields) {
/* we did not find the sufficient number of fields to recognize this if(!badcookie && (7 != fields))
as a valid line, abort and go home */ /* we did not find the sufficient number of fields */
free_cookiemess(co); badcookie = TRUE;
if(badcookie) {
freecookie(co);
return NULL; return NULL;
} }
} }
if(!c->running && /* read from a file */ if(!c->running && /* read from a file */
c->newsession && /* clean session cookies */ c->newsession && /* clean session cookies */
!co->expires) { /* this is a session cookie since it doesn't expire! */ !co->expires) { /* this is a session cookie since it doesn't expire! */
free_cookiemess(co); freecookie(co);
return NULL; return NULL;
} }
@ -509,16 +551,7 @@ Curl_cookie_add(struct SessionHandle *data,
live cookies stay alive */ live cookies stay alive */
/* Free the newcomer and get out of here! */ /* Free the newcomer and get out of here! */
if(co->domain) freecookie(co);
free(co->domain);
if(co->path)
free(co->path);
if(co->name)
free(co->name);
if(co->value)
free(co->value);
free(co);
return NULL; return NULL;
} }
@ -764,24 +797,8 @@ void Curl_cookie_cleanup(struct CookieInfo *c)
co = c->cookies; co = c->cookies;
while(co) { while(co) {
if(co->name)
free(co->name);
if(co->value)
free(co->value);
if(co->domain)
free(co->domain);
if(co->path)
free(co->path);
if(co->expirestr)
free(co->expirestr);
if(co->version)
free(co->version);
if(co->maxage)
free(co->maxage);
next = co->next; next = co->next;
free(co); freecookie(co);
co = next; co = next;
} }
free(c); /* free the base struct as well */ free(c); /* free the base struct as well */