[Asterisk-code-review] res config ldap: Make memory allocation more consistent (asterisk[13])
Joshua Colp
asteriskteam at digium.com
Tue Feb 21 16:34:57 CST 2017
Joshua Colp has submitted this change and it was merged. ( https://gerrit.asterisk.org/5006 )
Change subject: res_config_ldap: Make memory allocation more consistent
......................................................................
res_config_ldap: Make memory allocation more consistent
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
---
M res/res_config_ldap.c
1 file changed, 39 insertions(+), 58 deletions(-)
Approvals:
Mark Michelson: Looks good to me, approved
Anonymous Coward #1000019: Verified
Joshua Colp: Looks good to me, but someone else must approve
diff --git a/res/res_config_ldap.c b/res/res_config_ldap.c
index 9d34e57..2244677 100644
--- a/res/res_config_ldap.c
+++ b/res/res_config_ldap.c
@@ -124,7 +124,7 @@
if (table_name) {
if (!(p->table_name = ast_strdup(table_name))) {
- free(p);
+ ast_free(p);
return NULL;
}
}
@@ -238,7 +238,7 @@
if (c->attributes) {
ast_variables_destroy(c->attributes);
}
- free(c);
+ ast_free(c);
}
base_table_config = NULL;
@@ -424,7 +424,7 @@
* 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 @@
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 @@
}
}
- 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 @@
}
p++;
}
- free(vars);
+ ast_free(vars);
}
return var;
}
@@ -1066,7 +1061,7 @@
p++;
}
}
- free(vars);
+ ast_free(vars);
}
return cfg;
@@ -1138,7 +1133,7 @@
* 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 @@
}
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
}
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 @@
/* 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 @@
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 @@
}
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;
}
--
To view, visit https://gerrit.asterisk.org/5006
To unsubscribe, visit https://gerrit.asterisk.org/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: Iafec9c1fd8ea49ccc496d6316769a6a426daa804
Gerrit-PatchSet: 1
Gerrit-Project: asterisk
Gerrit-Branch: 13
Gerrit-Owner: Sean Bright <sean.bright at gmail.com>
Gerrit-Reviewer: Anonymous Coward #1000019
Gerrit-Reviewer: Joshua Colp <jcolp at digium.com>
Gerrit-Reviewer: Mark Michelson <mmichelson at digium.com>
More information about the asterisk-code-review
mailing list