From 5a75bafe6c7f81d83d668ca6184e2242418961c2 Mon Sep 17 00:00:00 2001 From: Greg Hudson Date: Thu, 5 Feb 2009 19:44:35 +0000 Subject: [PATCH] Change krb5_rc_resolve_type (not a public API) to allocate the rcache structure. Make output parameter values of krb5_rc_resolve_type and krb5_rc_default well-defined in case of errors. git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@21896 dc483132-0cff-0310-8789-dd5450dbe970 --- src/lib/krb5/rcache/rc_base.c | 68 +++++++++++++++++++---------------- src/lib/krb5/rcache/rc_dfl.c | 7 +--- 2 files changed, 38 insertions(+), 37 deletions(-) diff --git a/src/lib/krb5/rcache/rc_base.c b/src/lib/krb5/rcache/rc_base.c index ea532b3d7..43b901fac 100644 --- a/src/lib/krb5/rcache/rc_base.c +++ b/src/lib/krb5/rcache/rc_base.c @@ -64,24 +64,39 @@ krb5_error_code krb5_rc_register_type(krb5_context context, return 0; } -krb5_error_code krb5_rc_resolve_type(krb5_context context, krb5_rcache *id, +krb5_error_code krb5_rc_resolve_type(krb5_context context, krb5_rcache *idptr, char *type) { struct krb5_rc_typelist *t; krb5_error_code err; + krb5_rcache id; + + *idptr = NULL; + + /* Find the named type in the list. */ err = k5_mutex_lock(&rc_typelist_lock); if (err) return err; - for (t = typehead;t && strcmp(t->ops->type,type);t = t->next) + for (t = typehead; t && strcmp(t->ops->type, type); t = t->next) ; - if (!t) { - k5_mutex_unlock(&rc_typelist_lock); + k5_mutex_unlock(&rc_typelist_lock); + if (!t) return KRB5_RC_TYPE_NOTFOUND; + + /* Create and return the rcache structure. */ + id = malloc(sizeof(*id)); + if (!id) + return KRB5_RC_MALLOC; + err = k5_mutex_init(&id->lock); + if (err) { + free(id); + return err; } - /* allocate *id? nah */ - (*id)->ops = t->ops; - k5_mutex_unlock(&rc_typelist_lock); - return k5_mutex_init(&(*id)->lock); + id->data = NULL; /* Gets real data when resolved */ + id->magic = 0; /* Gets real magic after resolved */ + id->ops = t->ops; + *idptr = id; + return 0; } char * krb5_rc_get_type(krb5_context context, krb5_rcache id) @@ -108,25 +123,23 @@ char * krb5_rc_default_name(krb5_context context) } krb5_error_code -krb5_rc_default(krb5_context context, krb5_rcache *id) +krb5_rc_default(krb5_context context, krb5_rcache *idptr) { krb5_error_code retval; + krb5_rcache id; - if (!(*id = (krb5_rcache )malloc(sizeof(**id)))) - return KRB5_RC_MALLOC; - - if ((retval = krb5_rc_resolve_type(context, id, - krb5_rc_default_type(context)))) { - free(*id); + *idptr = NULL; + retval = krb5_rc_resolve_type(context, &id, krb5_rc_default_type(context)); + if (retval) return retval; - } - if ((retval = krb5_rc_resolve(context, *id, - krb5_rc_default_name(context)))) { - k5_mutex_destroy(&(*id)->lock); - free(*id); + retval = krb5_rc_resolve(context, id, krb5_rc_default_name(context)); + if (retval) { + k5_mutex_destroy(&id->lock); + free(id); return retval; } - (*id)->magic = KV5M_RCACHE; + id->magic = KV5M_RCACHE; + *idptr = id; return retval; } @@ -151,17 +164,10 @@ krb5_error_code krb5_rc_resolve_full(krb5_context context, krb5_rcache *idptr, (void) strncpy(type, string_name, diff); type[residual - string_name] = '\0'; - if (!(id = (krb5_rcache) malloc(sizeof(*id)))) { - free(type); - return KRB5_RC_MALLOC; - } - - if ((retval = krb5_rc_resolve_type(context, &id,type))) { - free(type); - free(id); - return retval; - } + retval = krb5_rc_resolve_type(context, &id,type); free(type); + if (retval) + return retval; if ((retval = krb5_rc_resolve(context, id,residual + 1))) { k5_mutex_destroy(&id->lock); free(id); diff --git a/src/lib/krb5/rcache/rc_dfl.c b/src/lib/krb5/rcache/rc_dfl.c index 009edfdad..91eaaea94 100644 --- a/src/lib/krb5/rcache/rc_dfl.c +++ b/src/lib/krb5/rcache/rc_dfl.c @@ -826,14 +826,9 @@ krb5_rc_dfl_expunge_locked(krb5_context context, krb5_rcache id) t = (struct dfl_data *)id->data; /* point to recovered cache */ } - tmp = (krb5_rcache) malloc(sizeof(*tmp)); - if (!tmp) - return ENOMEM; retval = krb5_rc_resolve_type(context, &tmp, "dfl"); - if (retval) { - free(tmp); + if (retval) return retval; - } retval = krb5_rc_resolve(context, tmp, 0); if (retval) goto cleanup; -- 2.26.2