[Asterisk-cvs] asterisk chanvars.c,1.11,1.12

kpfleming kpfleming
Wed Oct 5 17:15:36 CDT 2005


Update of /usr/cvsroot/asterisk
In directory mongoose.digium.com:/tmp/cvs-serv8965

Modified Files:
	chanvars.c 
Log Message:
clean up some poorly constructed code (issue #5399, with mod to include header file for ast_copy_string())


Index: chanvars.c
===================================================================
RCS file: /usr/cvsroot/asterisk/chanvars.c,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -d -r1.11 -r1.12
--- chanvars.c	14 Sep 2005 20:46:49 -0000	1.11
+++ chanvars.c	5 Oct 2005 21:11:17 -0000	1.12
@@ -31,44 +31,32 @@
 
 #include "asterisk/chanvars.h"
 #include "asterisk/logger.h"
+#include "asterisk/strings.h"
 
 struct ast_var_t *ast_var_assign(const char *name, const char *value)
 {
 	int i;
 	struct ast_var_t *var;
-	int len;
-	
-	len = sizeof(struct ast_var_t);
-	
-	len += strlen(name) + 1;
-	len += strlen(value) + 1;
 	
-	var = malloc(len);
+	var = calloc(sizeof(struct ast_var_t) + strlen(name) + 1 + strlen(value) + 1, sizeof(char));
 
-	if (var == NULL)
-	{
+	if (var == NULL) {
 		ast_log(LOG_WARNING, "Out of memory\n");
 		return NULL;
 	}
-	
-	memset(var, 0, len);
-	i = strlen(name);
-	strncpy(var->name, name, i); 
-	var->name[i] = '\0';
 
-	var->value = var->name + i + 1;
-
-	i = strlen(value);
-	strncpy(var->value, value, i);
-	var->value[i] = '\0';
+	i = strlen(name) + 1;
+	ast_copy_string(var->name, name, i);
+	var->value = var->name + i;
+	ast_copy_string(var->value, value, strlen(value) + 1);
 	
 	return var;
 }	
 	
 void ast_var_delete(struct ast_var_t *var)
 {
-	if (var == NULL) return;
-	free(var);
+	if (var)
+		free(var);
 }
 
 char *ast_var_name(struct ast_var_t *var)
@@ -80,8 +68,8 @@
 	if (var->name == NULL)
 		return NULL;
 	/* Return the name without the initial underscores */
-	if ((strlen(var->name) > 0) && (var->name[0] == '_')) {
-		if ((strlen(var->name) > 1) && (var->name[1] == '_'))
+	if (var->name[0] == '_') {
+		if (var->name[1] == '_')
 			name = (char*)&(var->name[2]);
 		else
 			name = (char*)&(var->name[1]);
@@ -92,12 +80,12 @@
 
 char *ast_var_full_name(struct ast_var_t *var)
 {
-	return (var != NULL ? var->name : NULL);
+	return (var ? var->name : NULL);
 }
 
 char *ast_var_value(struct ast_var_t *var)
 {
-	return (var != NULL ? var->value : NULL);
+	return (var ? var->value : NULL);
 }
 
-	
+




More information about the svn-commits mailing list