[asterisk-commits] trunk r9310 - in /trunk: cli.c config.c db.c devicestate.c dnsmgr.c

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Thu Feb 9 09:59:54 MST 2006


Author: russell
Date: Thu Feb  9 10:59:50 2006
New Revision: 9310

URL: http://svn.digium.com/view/asterisk?rev=9310&view=rev
Log:
conversions to memory allocation wrappers, remove duplicated error messages,
remove unnecessary casts, malloc+memset to calloc (issue #6395)

Modified:
    trunk/cli.c
    trunk/config.c
    trunk/db.c
    trunk/devicestate.c
    trunk/dnsmgr.c

Modified: trunk/cli.c
URL: http://svn.digium.com/view/asterisk/trunk/cli.c?rev=9310&r1=9309&r2=9310&view=diff
==============================================================================
--- trunk/cli.c (original)
+++ trunk/cli.c Thu Feb  9 10:59:50 2006
@@ -63,14 +63,14 @@
 void ast_cli(int fd, char *fmt, ...)
 {
 	char *stuff;
-	int res = 0;
+	int res;
 	va_list ap;
 
 	va_start(ap, fmt);
 	res = vasprintf(&stuff, fmt, ap);
 	va_end(ap);
 	if (res == -1) {
-		ast_log(LOG_ERROR, "Out of memory\n");
+		ast_log(LOG_ERROR, "Memory allocation failure\n");
 	} else {
 		ast_carefulwrite(fd, stuff, strlen(stuff), 100);
 		free(stuff);
@@ -520,8 +520,7 @@
 
 	if (argc != 4)
 		return RESULT_SHOWUSAGE;
-	buf = malloc(buflen);
-	if (!buf)
+	if (!(buf = ast_malloc(buflen)))
 		return RESULT_FAILURE;
 	buf[len] = '\0';
 	matches = ast_cli_completion_matches(argv[2], argv[3]);
@@ -534,9 +533,8 @@
 			if (len + matchlen >= buflen) {
 				buflen += matchlen * 3;
 				obuf = buf;
-				buf = realloc(obuf, buflen);
-				if (!buf) 
-					/* Out of memory...  Just free old buffer and be done */
+				if (!(buf = ast_realloc(obuf, buflen))) 
+					/* Memory allocation failure...  Just free old buffer and be done */
 					free(obuf);
 			}
 			if (buf)
@@ -1274,13 +1272,14 @@
 	while ((retstr = ast_cli_generator(text, word, matches)) != NULL) {
 		if (matches + 1 >= match_list_len) {
 			match_list_len <<= 1;
-			match_list = realloc(match_list, match_list_len * sizeof(char *));
+			if (!(match_list = ast_realloc(match_list, match_list_len * sizeof(*match_list))))
+				return NULL;
 		}
 		match_list[++matches] = retstr;
 	}
 
 	if (!match_list)
-		return (char **) NULL;
+		return NULL;
 
 	which = 2;
 	prevstr = match_list[1];
@@ -1291,14 +1290,18 @@
 		max_equal = i;
 	}
 
-	retstr = malloc(max_equal + 1);
+	if (!(retstr = ast_malloc(max_equal + 1)))
+		return NULL;
+	
 	strncpy(retstr, match_list[1], max_equal);
 	retstr[max_equal] = '\0';
 	match_list[0] = retstr;
 
-	if (matches + 1 >= match_list_len)
-		match_list = realloc(match_list, (match_list_len + 1) * sizeof(char *));
-	match_list[matches + 1] = (char *) NULL;
+	if (matches + 1 >= match_list_len) {
+		if (!(match_list = ast_realloc(match_list, (match_list_len + 1) * sizeof(*match_list))))
+			return NULL;
+	}
+	match_list[matches + 1] = NULL;
 
 	return match_list;
 }
@@ -1391,10 +1394,9 @@
 	int x;
 	char *dup;
 	int tws;
-
-	dup = parse_args(s, &x, argv, sizeof(argv) / sizeof(argv[0]), &tws);
-	if (!dup) {
-		ast_log(LOG_ERROR, "Out of Memory!\n");
+	
+	if (!(dup = parse_args(s, &x, argv, sizeof(argv) / sizeof(argv[0]), &tws))) {
+		ast_log(LOG_ERROR, "Memory allocation failure\n");
 		return -1;
 	}
 

Modified: trunk/config.c
URL: http://svn.digium.com/view/asterisk/trunk/config.c?rev=9310&r1=9309&r2=9310&view=diff
==============================================================================
--- trunk/config.c (original)
+++ trunk/config.c Thu Feb  9 10:59:50 2006
@@ -101,13 +101,11 @@
 struct ast_variable *ast_variable_new(const char *name, const char *value) 
 {
 	struct ast_variable *variable;
-
-	int length = strlen(name) + strlen(value) + 2 + sizeof(struct ast_variable);
-	variable = malloc(length);
-	if (variable) {
-		memset(variable, 0, length);
+	int name_len = strlen(name) + 1;	
+
+	if ((variable = ast_calloc(1, name_len + strlen(value) + 1 + sizeof(*variable)))) {
 		variable->name = variable->stuff;
-		variable->value = variable->stuff + strlen(name) + 1;		
+		variable->value = variable->stuff + name_len;		
 		strcpy(variable->name,name);
 		strcpy(variable->value,value);
 	}
@@ -203,9 +201,7 @@
 {
 	struct ast_category *category;
 
-	category = malloc(sizeof(struct ast_category));
-	if (category) {
-		memset(category, 0, sizeof(struct ast_category));
+	if ((category = ast_calloc(1, sizeof(*category)))) {		
 		ast_copy_string(category->name, name, sizeof(category->name));
 	}
 
@@ -329,9 +325,7 @@
 {
 	struct ast_config *config;
 
-	config = malloc(sizeof(*config));
-	if (config) {
-		memset(config, 0, sizeof(*config));
+	if ((config = ast_calloc(1, sizeof(*config)))) {
 		config->max_include_level = MAX_INCLUDE_LEVEL;
 	}
 
@@ -390,9 +384,7 @@
  		if (*c++ != '(')
  			c = NULL;
 		catname = cur;
-		*cat = newcat = ast_category_new(catname);
-		if (!newcat) {
-			ast_log(LOG_WARNING, "Out of memory, line %d of %s\n", lineno, configfile);
+		if (!(*cat = newcat = ast_category_new(catname))) {
 			return -1;
 		}
  		/* If there are options or categories to inherit from, process them now */
@@ -511,15 +503,13 @@
 				c++;
 			} else
 				object = 0;
-			v = ast_variable_new(ast_strip(cur), ast_strip(c));
-			if (v) {
+			if ((v = ast_variable_new(ast_strip(cur), ast_strip(c)))) {
 				v->lineno = lineno;
 				v->object = object;
 				/* Put and reset comments */
 				v->blanklines = 0;
 				ast_variable_append(*cat, v);
 			} else {
-				ast_log(LOG_WARNING, "Out of memory, line %d\n", lineno);
 				return -1;
 			}
 		} else {
@@ -767,12 +757,10 @@
 	length += strlen(database) + 1;
 	if (table)
 		length += strlen(table) + 1;
-	map = malloc(length);
-
-	if (!map)
+
+	if (!(map = ast_calloc(1, length)))
 		return -1;
 
-	memset(map, 0, length);
 	map->name = map->stuff;
 	strcpy(map->name, name);
 	map->driver = map->name + strlen(map->name) + 1;

Modified: trunk/db.c
URL: http://svn.digium.com/view/asterisk/trunk/db.c?rev=9310&r1=9309&r2=9310&view=diff
==============================================================================
--- trunk/db.c (original)
+++ trunk/db.c Thu Feb  9 10:59:50 2006
@@ -63,14 +63,11 @@
 
 static int dbinit(void) 
 {
-	if (!astdb) {
-		if (!(astdb = dbopen((char *)ast_config_AST_DB, O_CREAT | O_RDWR, 0664, DB_BTREE, NULL))) {
-			ast_log(LOG_WARNING, "Unable to open Asterisk database\n");
-		}
-	}
-	if (astdb)
-		return 0;
-	return -1;
+	if (!astdb && !(astdb = dbopen((char *)ast_config_AST_DB, O_CREAT | O_RDWR, 0664, DB_BTREE, NULL))) {
+		ast_log(LOG_WARNING, "Unable to open Asterisk database\n");
+		return -1;
+	}
+	return 0;
 }
 
 
@@ -402,6 +399,7 @@
 	char prefix[256];
 	DBT key, data;
 	char *keys, *values;
+	int values_len;
 	int res;
 	int pass;
 	struct ast_db_entry *last = NULL;
@@ -440,20 +438,18 @@
 		} else {
 			values = "<bad value>";
 		}
-		if (keymatch(keys, prefix)) {
-			cur = malloc(sizeof(struct ast_db_entry) + strlen(keys) + strlen(values) + 2);
-			if (cur) {
-				cur->next = NULL;
-				cur->key = cur->data + strlen(values) + 1;
-				strcpy(cur->data, values);
-				strcpy(cur->key, keys);
-				if (last) {
-					last->next = cur;
-				} else {
-					ret = cur;
-				}
-				last = cur;
+		values_len = strlen(values) + 1;
+		if (keymatch(keys, prefix) && (cur = ast_malloc(sizeof(*cur) + strlen(keys) + 1 + values_len))) {
+			cur->next = NULL;
+			cur->key = cur->data + values_len;
+			strcpy(cur->data, values);
+			strcpy(cur->key, keys);
+			if (last) {
+				last->next = cur;
+			} else {
+				ret = cur;
 			}
+			last = cur;
 		}
 	}
 	ast_mutex_unlock(&dblock);

Modified: trunk/devicestate.c
URL: http://svn.digium.com/view/asterisk/trunk/devicestate.c?rev=9310&r1=9309&r2=9310&view=diff
==============================================================================
--- trunk/devicestate.c (original)
+++ trunk/devicestate.c Thu Feb  9 10:59:50 2006
@@ -143,11 +143,7 @@
 {
 	struct devstate_cb *devcb;
 
-	if (!callback)
-		return -1;
-
-	devcb = calloc(1, sizeof(*devcb));
-	if (!devcb)
+	if (!callback || !(devcb = ast_calloc(1, sizeof(*devcb))))
 		return -1;
 
 	devcb->data = data;
@@ -198,16 +194,13 @@
 static int __ast_device_state_changed_literal(char *buf)
 {
 	char *device, *tmp;
-	struct state_change *change = NULL;
+	struct state_change *change;
 
 	device = buf;
-	tmp = strrchr(device, '-');
-	if (tmp)
+	if ((tmp = strrchr(device, '-')))
 		*tmp = '\0';
-	if (change_thread != AST_PTHREADT_NULL)
-		change = calloc(1, sizeof(*change) + strlen(device));
-
-	if (!change) {
+
+	if (change_thread == AST_PTHREADT_NULL || !(change = ast_calloc(1, sizeof(*change) + strlen(device)))) {
 		/* we could not allocate a change struct, or */
 		/* there is no background thread, so process the change now */
 		do_state_change(device);

Modified: trunk/dnsmgr.c
URL: http://svn.digium.com/view/asterisk/trunk/dnsmgr.c?rev=9310&r1=9309&r2=9310&view=diff
==============================================================================
--- trunk/dnsmgr.c (original)
+++ trunk/dnsmgr.c Thu Feb  9 10:59:50 2006
@@ -83,11 +83,7 @@
 {
 	struct ast_dnsmgr_entry *entry;
 
-	if (!result || ast_strlen_zero(name))
-		return NULL;
-
-	entry = calloc(1, sizeof(*entry) + strlen(name));
-	if (!entry)
+	if (!result || ast_strlen_zero(name) || !(entry = ast_calloc(1, sizeof(*entry) + strlen(name))))
 		return NULL;
 
 	entry->result = result;
@@ -285,8 +281,7 @@
 
 int dnsmgr_init(void)
 {
-	sched = sched_context_create();
-	if (!sched) {
+	if (!(sched = sched_context_create())) {
 		ast_log(LOG_ERROR, "Unable to create schedule context.\n");
 		return -1;
 	}



More information about the asterisk-commits mailing list