]> git.ipfire.org Git - thirdparty/asterisk.git/commitdiff
res_config_ldap: Make memory allocation more consistent 06/5006/1
authorSean Bright <sean.bright@gmail.com>
Mon, 20 Feb 2017 11:45:54 +0000 (06:45 -0500)
committerSean Bright <sean.bright@gmail.com>
Mon, 20 Feb 2017 11:45:54 +0000 (06:45 -0500)
The code in update_ldap() and update2_ldap() was using both Asterisk's
memory allocation routines as well as OpenLDAP's. I've changed it so
that everything that is passed to OpenLDAP's functions are allocated
with their routines.

Change-Id: Iafec9c1fd8ea49ccc496d6316769a6a426daa804

res/res_config_ldap.c

index 9d34e570980678cd5570bf18206576b36e7bc5e6..2244677229fad9c5896895706f3765d70f22c6a8 100644 (file)
@@ -124,7 +124,7 @@ static struct ldap_table_config *table_config_new(const char *table_name)
 
        if (table_name) {
                if (!(p->table_name = ast_strdup(table_name))) {
-                       free(p);
+                       ast_free(p);
                        return NULL;
                }
        }
@@ -238,7 +238,7 @@ static void table_configs_free(void)
                if (c->attributes) {
                        ast_variables_destroy(c->attributes);
                }
-               free(c);
+               ast_free(c);
        }
 
        base_table_config = NULL;
@@ -424,7 +424,7 @@ static struct ast_variable **realtime_ldap_result_to_vars(struct ldap_table_conf
         * value in \a variable_value; otherwise, we keep \a vars static and increase the length of the linked list of variables in the array element.
         * This memory must be freed outside of this function.
         */
-       vars = ast_calloc(sizeof(struct ast_variable *), tot_count + 1);
+       vars = ast_calloc(tot_count + 1, sizeof(struct ast_variable *));
 
        ldap_entry = ldap_first_entry(ldapConn, ldap_result_msg);
 
@@ -516,14 +516,14 @@ static struct ast_variable **realtime_ldap_result_to_vars(struct ldap_table_conf
                                                                        is_delimited = 0;
                                                                        pos = 0;
                                                                }
-                                                               free(delim_value);
+                                                               ast_free(delim_value);
                                                                delim_value = NULL;
 
                                                                ast_debug(4, "LINE(%d) DELIM pos: %d i: %d\n", __LINE__, pos, i);
                                                        } else {
                                                                /* not delimited */
                                                                if (delim_value) {
-                                                                       free(delim_value);
+                                                                       ast_free(delim_value);
                                                                        delim_value = NULL;
                                                                }
                                                                ast_debug(2, "LINE(%d) attribute_name: %s value: %s\n", __LINE__, attribute_name, valptr);
@@ -928,13 +928,8 @@ static struct ast_variable **realtime_ldap_base_ap(unsigned int *entries_count_p
                }
        }
 
-       if (filter) {
-               ast_free(filter);
-       }
-
-       if (clean_basedn) {
-               ast_free(clean_basedn);
-       }
+       ast_free(filter);
+       ast_free(clean_basedn);
 
        ast_mutex_unlock(&ldap_lock);
 
@@ -1007,7 +1002,7 @@ static struct ast_variable *realtime_ldap(const char *basedn,
                        }
                        p++;
                }
-               free(vars);
+               ast_free(vars);
        }
        return var;
 }
@@ -1066,7 +1061,7 @@ static struct ast_config *realtime_multi_ldap(const char *basedn,
                                p++;
                        }
                }
-               free(vars);
+               ast_free(vars);
        }
        return cfg;
 
@@ -1138,7 +1133,7 @@ static struct ast_config *config_ldap(const char *basedn, const char *table_name
         * first, and since the data could easily exceed stack size, this is
         * allocated from the heap.
         */
-       if (!(categories = ast_calloc(sizeof(*categories), vars_count))) {
+       if (!(categories = ast_calloc(vars_count, sizeof(*categories)))) {
                return NULL;
        }
 
@@ -1292,7 +1287,7 @@ static int update_ldap(const char *basedn, const char *table_name, const char *a
        }
 
        mods_size = 2; /* one for the first param/value pair and one for the the terminating NULL */
-       ldap_mods = ldap_memcalloc(sizeof(LDAPMod *), mods_size);
+       ldap_mods = ldap_memcalloc(mods_size, sizeof(LDAPMod *));
        ldap_mods[0] = ldap_memcalloc(1, sizeof(LDAPMod));
        ldap_mods[0]->mod_type = ldap_strdup(newparam);
 
@@ -1300,7 +1295,7 @@ static int update_ldap(const char *basedn, const char *table_name, const char *a
                ldap_mods[0]->mod_op = LDAP_MOD_DELETE;
        } else {
                ldap_mods[0]->mod_op = LDAP_MOD_REPLACE;
-               ldap_mods[0]->mod_values = ast_calloc(sizeof(char *), 2);
+               ldap_mods[0]->mod_values = ldap_memcalloc(2, sizeof(char *));
                ldap_mods[0]->mod_values[0] = ldap_strdup(field->value);
        }
 
@@ -1314,7 +1309,7 @@ static int update_ldap(const char *basedn, const char *table_name, const char *a
                                ldap_mods[i]->mod_values[0] = ldap_memrealloc(ldap_mods[i]->mod_values[0], sizeof(char) * (strlen(ldap_mods[i]->mod_values[0]) + strlen(field->value) + 2));
                                strcat(ldap_mods[i]->mod_values[0], ";");
                                strcat(ldap_mods[i]->mod_values[0], field->value);
-                               mod_exists = 1; 
+                               mod_exists = 1;
                                break;
                        }
                }
@@ -1323,22 +1318,19 @@ static int update_ldap(const char *basedn, const char *table_name, const char *a
                if (!mod_exists) {
                        mods_size++;
                        ldap_mods = ldap_memrealloc(ldap_mods, sizeof(LDAPMod *) * mods_size);
-                       ldap_mods[mods_size - 1] = NULL;
-                       
                        ldap_mods[mods_size - 2] = ldap_memcalloc(1, sizeof(LDAPMod));
-
-                       ldap_mods[mods_size - 2]->mod_type = ldap_memcalloc(sizeof(char), strlen(newparam) + 1);
-                       strcpy(ldap_mods[mods_size - 2]->mod_type, newparam);
+                       ldap_mods[mods_size - 2]->mod_type = ldap_strdup(newparam);
 
                        if (strlen(field->value) == 0) {
                                ldap_mods[mods_size - 2]->mod_op = LDAP_MOD_DELETE;
                        } else {
                                ldap_mods[mods_size - 2]->mod_op = LDAP_MOD_REPLACE;
-
-                               ldap_mods[mods_size - 2]->mod_values = ldap_memcalloc(sizeof(char *), 2);
-                               ldap_mods[mods_size - 2]->mod_values[0] = ldap_memcalloc(sizeof(char), strlen(field->value) + 1);
-                               strcpy(ldap_mods[mods_size - 2]->mod_values[0], field->value);
+                               ldap_mods[mods_size - 2]->mod_values = ldap_memcalloc(2, sizeof(char *));
+                               ldap_mods[mods_size - 2]->mod_values[0] = ldap_strdup(field->value);
                        }
+
+                       /* NULL terminate */
+                       ldap_mods[mods_size - 1] = NULL;
                }
        }
        /* freeing ldap_mods further down */
@@ -1368,10 +1360,10 @@ static int update_ldap(const char *basedn, const char *table_name, const char *a
                ast_log(LOG_WARNING, "Query: %s\n", ast_str_buffer(filter));
 
                ast_mutex_unlock(&ldap_lock);
-               free(filter);
-               free(clean_basedn);
+               ast_free(filter);
+               ast_free(clean_basedn);
                ldap_msgfree(ldap_result_msg);
-               ldap_mods_free(ldap_mods, 0);
+               ldap_mods_free(ldap_mods, 1);
                return -1;
        }
        /* Ready to update */
@@ -1401,7 +1393,7 @@ static int update_ldap(const char *basedn, const char *table_name, const char *a
        ast_free(filter);
        ast_free(clean_basedn);
        ldap_msgfree(ldap_result_msg);
-       ldap_mods_free(ldap_mods, 0);
+       ldap_mods_free(ldap_mods, 1);
        return num_entries;
 }
 
@@ -1481,16 +1473,12 @@ static int update2_ldap(const char *basedn, const char *table_name, const struct
        }
 
        mods_size = 2; /* one for the first param/value pair and one for the the terminating NULL */
-       ldap_mods = ast_calloc(sizeof(LDAPMod *), mods_size);
-       ldap_mods[0] = ast_calloc(1, sizeof(LDAPMod));
-
+       ldap_mods = ldap_memcalloc(mods_size, sizeof(LDAPMod *));
+       ldap_mods[0] = ldap_memcalloc(1, sizeof(LDAPMod));
        ldap_mods[0]->mod_op = LDAP_MOD_REPLACE;
-       ldap_mods[0]->mod_type = ast_calloc(sizeof(char), strlen(newparam) + 1);
-       strcpy(ldap_mods[0]->mod_type, newparam);
-
-       ldap_mods[0]->mod_values = ast_calloc(sizeof(char), 2);
-       ldap_mods[0]->mod_values[0] = ast_calloc(sizeof(char), strlen(field->value) + 1);
-       strcpy(ldap_mods[0]->mod_values[0], field->value);
+       ldap_mods[0]->mod_type = ldap_strdup(newparam);
+       ldap_mods[0]->mod_values = ldap_memcalloc(2, sizeof(char *));
+       ldap_mods[0]->mod_values[0] = ldap_strdup(field->value);
 
        while ((field = field->next)) {
                newparam = convert_attribute_name_to_ldap(table_config, field->name);
@@ -1510,18 +1498,15 @@ static int update2_ldap(const char *basedn, const char *table_name, const struct
                /* create new mod */
                if (!mod_exists) {
                        mods_size++;
-                       ldap_mods = ast_realloc(ldap_mods, sizeof(LDAPMod *) * mods_size);
-                       ldap_mods[mods_size - 1] = NULL;
-                       ldap_mods[mods_size - 2] = ast_calloc(1, sizeof(LDAPMod));
-
+                       ldap_mods = ldap_memrealloc(ldap_mods, sizeof(LDAPMod *) * mods_size);
+                       ldap_mods[mods_size - 2] = ldap_memcalloc(1, sizeof(LDAPMod));
                        ldap_mods[mods_size - 2]->mod_op = LDAP_MOD_REPLACE;
+                       ldap_mods[mods_size - 2]->mod_type = ldap_strdup(newparam);
+                       ldap_mods[mods_size - 2]->mod_values = ldap_memcalloc(2, sizeof(char *));
+                       ldap_mods[mods_size - 2]->mod_values[0] = ldap_strdup(field->value);
 
-                       ldap_mods[mods_size - 2]->mod_type = ast_calloc(sizeof(char), strlen(newparam) + 1);
-                       strcpy(ldap_mods[mods_size - 2]->mod_type, newparam);
-
-                       ldap_mods[mods_size - 2]->mod_values = ast_calloc(sizeof(char *), 2);
-                       ldap_mods[mods_size - 2]->mod_values[0] = ast_calloc(sizeof(char), strlen(field->value) + 1);
-                       strcpy(ldap_mods[mods_size - 2]->mod_values[0], field->value);
+                       /* NULL terminate */
+                       ldap_mods[mods_size - 1] = NULL;
                }
        }
        /* freeing ldap_mods further down */
@@ -1555,7 +1540,7 @@ static int update2_ldap(const char *basedn, const char *table_name, const struct
                ast_free(filter);
                ast_free(clean_basedn);
                ldap_msgfree(ldap_result_msg);
-               ldap_mods_free(ldap_mods, 0);
+               ldap_mods_free(ldap_mods, 1);
                return -1;
        }
        /* Ready to update */
@@ -1577,14 +1562,10 @@ static int update2_ldap(const char *basedn, const char *table_name, const struct
        }
 
        ast_mutex_unlock(&ldap_lock);
-       if (filter) {
-               ast_free(filter);
-       }
-       if (clean_basedn) {
-               ast_free(clean_basedn);
-       }
+       ast_free(filter);
+       ast_free(clean_basedn);
        ldap_msgfree(ldap_result_msg);
-       ldap_mods_free(ldap_mods, 0);
+       ldap_mods_free(ldap_mods, 1);
        return num_entries;
 }