[asterisk-commits] res config ldap: Make memory allocation more consistent (asterisk[master])

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Tue Feb 21 18:58:26 CST 2017


Anonymous Coward #1000019 has submitted this change and it was merged. ( https://gerrit.asterisk.org/5016 )

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, 31 insertions(+), 50 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 9ad4329..5362e92 100644
--- a/res/res_config_ldap.c
+++ b/res/res_config_ldap.c
@@ -422,7 +422,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);
 
@@ -926,13 +926,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);
 
@@ -1136,7 +1131,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;
 	}
 
@@ -1290,7 +1285,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);
 
@@ -1298,7 +1293,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);
 	}
 
@@ -1312,7 +1307,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;
 			}
 		}
@@ -1321,22 +1316,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 */
@@ -1369,7 +1361,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 */
@@ -1399,7 +1391,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;
 }
 
@@ -1479,16 +1471,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);
@@ -1508,18 +1496,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 */
@@ -1553,7 +1538,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 */
@@ -1575,14 +1560,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/5016
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Iafec9c1fd8ea49ccc496d6316769a6a426daa804
Gerrit-PatchSet: 1
Gerrit-Project: asterisk
Gerrit-Branch: master
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-commits mailing list