svn rev #25840: trunk/src/lib/krb5/krb/

ghudson@MIT.EDU ghudson at MIT.EDU
Mon Apr 30 19:33:39 EDT 2012


http://src.mit.edu/fisheye/changelog/krb5/?cs=25840
Commit By: ghudson
Log Message:
Simplify preauth2 helper functions using realloc


Changed Files:
U   trunk/src/lib/krb5/krb/preauth2.c
Modified: trunk/src/lib/krb5/krb/preauth2.c
===================================================================
--- trunk/src/lib/krb5/krb/preauth2.c	2012-04-27 22:40:21 UTC (rev 25839)
+++ trunk/src/lib/krb5/krb/preauth2.c	2012-04-30 23:33:38 UTC (rev 25840)
@@ -312,65 +312,36 @@
 {
     int i;
     krb5_enctype *ktypes;
+
     for (i = 0; i < *out_nktypes; i++) {
         if ((*out_ktypes)[i] == ktype)
             return;
     }
-    ktypes = malloc((*out_nktypes + 2) * sizeof(ktype));
-    if (ktypes) {
-        for (i = 0; i < *out_nktypes; i++)
-            ktypes[i] = (*out_ktypes)[i];
-        ktypes[i++] = ktype;
-        ktypes[i] = 0;
-        free(*out_ktypes);
+    ktypes = realloc(*out_ktypes, (*out_nktypes + 2) * sizeof(ktype));
+    if (ktypes != NULL) {
         *out_ktypes = ktypes;
-        *out_nktypes = i;
+        ktypes[(*out_nktypes)++] = ktype;
+        ktypes[*out_nktypes] = 0;
     }
 }
 
-/*
- * Add the given list of pa_data items to the existing list of items.
- * Factored out here to make reading the do_preauth logic easier to read.
- */
+/* Add a list of new pa_data items to an existing list. */
 static int
 grow_pa_list(krb5_pa_data ***out_pa_list, int *out_pa_list_size,
              krb5_pa_data **addition, int num_addition)
 {
     krb5_pa_data **pa_list;
-    int i, j;
+    int i;
 
-    if (out_pa_list == NULL || addition == NULL) {
-        return EINVAL;
-    }
-
-    if (*out_pa_list == NULL) {
-        /* Allocate room for the new additions and a NULL terminator. */
-        pa_list = malloc((num_addition + 1) * sizeof(krb5_pa_data *));
-        if (pa_list == NULL)
-            return ENOMEM;
-        for (i = 0; i < num_addition; i++)
-            pa_list[i] = addition[i];
-        pa_list[i] = NULL;
-        *out_pa_list = pa_list;
-        *out_pa_list_size = num_addition;
-    } else {
-        /*
-         * Allocate room for the existing entries plus
-         * the new additions and a NULL terminator.
-         */
-        pa_list = malloc((*out_pa_list_size + num_addition + 1)
-                         * sizeof(krb5_pa_data *));
-        if (pa_list == NULL)
-            return ENOMEM;
-        for (i = 0; i < *out_pa_list_size; i++)
-            pa_list[i] = (*out_pa_list)[i];
-        for (j = 0; j < num_addition;)
-            pa_list[i++] = addition[j++];
-        pa_list[i] = NULL;
-        free(*out_pa_list);
-        *out_pa_list = pa_list;
-        *out_pa_list_size = i;
-    }
+    /* Allocate space for new entries and a null terminator. */
+    pa_list = realloc(*out_pa_list, (*out_pa_list_size + num_addition + 1) *
+                      sizeof(*pa_list));
+    if (pa_list == NULL)
+        return ENOMEM;
+    *out_pa_list = pa_list;
+    for (i = 0; i < num_addition; i++)
+        pa_list[(*out_pa_list_size)++] = addition[i];
+    pa_list[*out_pa_list_size] = NULL;
     return 0;
 }
 



More information about the cvs-krb5 mailing list