Better handling of memory allocation failure [CID-77]

This commit is contained in:
Love Hornquist Astrand
2009-07-30 12:27:19 +02:00
parent 60c9bca968
commit 25b0f731ab

View File

@@ -62,9 +62,8 @@ static int
make_path(krb5_context context, struct tr_realm *r, make_path(krb5_context context, struct tr_realm *r,
const char *from, const char *to) const char *from, const char *to)
{ {
const char *p;
struct tr_realm *path = r->next;
struct tr_realm *tmp; struct tr_realm *tmp;
const char *p;
if(strlen(from) < strlen(to)){ if(strlen(from) < strlen(to)){
const char *str; const char *str;
@@ -90,11 +89,12 @@ make_path(krb5_context context, struct tr_realm *r,
N_("malloc: out of memory", "")); N_("malloc: out of memory", ""));
return ENOMEM; return ENOMEM;
} }
tmp->next = path; tmp->next = r->next;
path = tmp; r->next = tmp;
path->realm = strdup(p); tmp->realm = strdup(p);
if(path->realm == NULL){ if(tmp->realm == NULL){
r->next = path; /* XXX */ r->next = tmp->next;
free(tmp);
krb5_set_error_message(context, ENOMEM, krb5_set_error_message(context, ENOMEM,
N_("malloc: out of memory", "")); N_("malloc: out of memory", ""));
return ENOMEM;; return ENOMEM;;
@@ -104,10 +104,9 @@ make_path(krb5_context context, struct tr_realm *r,
p = from + strlen(from); p = from + strlen(from);
while(1){ while(1){
while(p >= from && *p != '/') p--; while(p >= from && *p != '/') p--;
if(p == from) { if(p == from)
r->next = path; /* XXX */
return KRB5KDC_ERR_POLICY; return KRB5KDC_ERR_POLICY;
}
if(strncmp(to, from, p - from) == 0) if(strncmp(to, from, p - from) == 0)
break; break;
tmp = calloc(1, sizeof(*tmp)); tmp = calloc(1, sizeof(*tmp));
@@ -116,24 +115,24 @@ make_path(krb5_context context, struct tr_realm *r,
N_("malloc: out of memory", "")); N_("malloc: out of memory", ""));
return ENOMEM; return ENOMEM;
} }
tmp->next = path; tmp->next = r->next;
path = tmp; r->next = tmp;
path->realm = malloc(p - from + 1); tmp->realm = malloc(p - from + 1);
if(path->realm == NULL){ if(tmp->realm == NULL){
r->next = path; /* XXX */ r->next = tmp->next;
free(tmp);
krb5_set_error_message(context, ENOMEM, krb5_set_error_message(context, ENOMEM,
N_("malloc: out of memory", "")); N_("malloc: out of memory", ""));
return ENOMEM; return ENOMEM;
} }
memcpy(path->realm, from, p - from); memcpy(tmp->realm, from, p - from);
path->realm[p - from] = '\0'; tmp->realm[p - from] = '\0';
p--; p--;
} }
} else { } else {
krb5_clear_error_message (context); krb5_clear_error_message (context);
return KRB5KDC_ERR_POLICY; return KRB5KDC_ERR_POLICY;
} }
r->next = path;
return 0; return 0;
} }