In ktfile_common_resolve, set the output pointer to NULL on error, and
authorGreg Hudson <ghudson@mit.edu>
Thu, 30 Apr 2009 17:28:55 +0000 (17:28 +0000)
committerGreg Hudson <ghudson@mit.edu>
Thu, 30 Apr 2009 17:28:55 +0000 (17:28 +0000)
use a cleanup label instead of freeing the same resources in multiple
error handling blocks.

git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@22296 dc483132-0cff-0310-8789-dd5450dbe970

src/lib/krb5/keytab/kt_file.c

index 2aaa9b9ab281b2057967a096cc296504eec13def..bb2d838d3d2d4965d7619e85d99e9d661d1fa41b 100644 (file)
@@ -200,41 +200,45 @@ static krb5_error_code krb5_ktfileint_find_slot
 
 static krb5_error_code
 ktfile_common_resolve(krb5_context context, const char *name,
-                     krb5_keytab *id, const struct _krb5_kt_ops *ops)
+                     krb5_keytab *idptr, const struct _krb5_kt_ops *ops)
 {
-    krb5_ktfile_data *data;
-    krb5_error_code err;
+    krb5_ktfile_data *data = NULL;
+    krb5_error_code err = ENOMEM;
+    krb5_keytab id;
 
-    if ((*id = (krb5_keytab) malloc(sizeof(**id))) == NULL)
-       return(ENOMEM);
+    *idptr = NULL;
+
+    id = calloc(1, sizeof(*id));
+    if (id == NULL)
+       return ENOMEM;
     
-    (*id)->ops = ops;
-    if ((data = (krb5_ktfile_data *)malloc(sizeof(krb5_ktfile_data))) == NULL) {
-       free(*id);
-       return(ENOMEM);
-    }
+    id->ops = ops;
+    data = calloc(1, sizeof(krb5_ktfile_data));
+    if (data == NULL)
+       goto cleanup;
 
-    err = k5_mutex_init(&data->lock);
-    if (err) {
-       free(data);
-       free(*id);
-       return err;
-    }
+    data->name = strdup(name);
+    if (data->name == NULL)
+       goto cleanup;
 
-    if ((data->name = strdup(name)) == NULL) {
-       k5_mutex_destroy(&data->lock);
-       free(data);
-       free(*id);
-       return(ENOMEM);
-    }
+    err = k5_mutex_init(&data->lock);
+    if (err)
+       goto cleanup;
 
     data->openf = 0;
     data->version = 0;
     data->iter_count = 0;
 
-    (*id)->data = (krb5_pointer)data;
-    (*id)->magic = KV5M_KEYTAB;
-    return(0);
+    id->data = (krb5_pointer) data;
+    id->magic = KV5M_KEYTAB;
+    *idptr = id;
+    return 0;
+cleanup:
+    if (data)
+       free(data->name);
+    free(data);
+    free(id);
+    return err;
 }
 
 static krb5_error_code KRB5_CALLCONV