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

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Tue Feb 21 17:21:06 CST 2017


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

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 6bdc013..9b8ffcc 100644
--- a/res/res_config_ldap.c
+++ b/res/res_config_ldap.c
@@ -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);
 
@@ -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);
 
@@ -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 */
@@ -1371,7 +1363,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 */
@@ -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/5013
To unsubscribe, visit https://gerrit.asterisk.org/settings

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