[asterisk-commits] trunk r10066 - in /trunk: acl.c app.c asterisk.c

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Tue Feb 14 15:28:01 MST 2006


Author: kpfleming
Date: Tue Feb 14 16:28:01 2006
New Revision: 10066

URL: http://svn.digium.com/view/asterisk?rev=10066&view=rev
Log:
more memory allocation wrapper conversion (issue #6365)

Modified:
    trunk/acl.c
    trunk/app.c
    trunk/asterisk.c

Modified: trunk/acl.c
URL: http://svn.digium.com/view/asterisk/trunk/acl.c?rev=10066&r1=10065&r2=10066&view=diff
==============================================================================
--- trunk/acl.c (original)
+++ trunk/acl.c Tue Feb 14 16:28:01 2006
@@ -113,9 +113,12 @@
 /* Create duplicate of ha structure */
 static struct ast_ha *ast_duplicate_ha(struct ast_ha *original)
 {
-	struct ast_ha *new_ha = malloc(sizeof(struct ast_ha));
-	/* Copy from original to new object */
-	ast_copy_ha(original, new_ha); 
+	struct ast_ha *new_ha;
+
+	if ((new_ha = ast_malloc(sizeof(*new_ha)))) {
+		/* Copy from original to new object */
+		ast_copy_ha(original, new_ha);
+	}
 
 	return new_ha;
 }
@@ -144,19 +147,20 @@
 
 struct ast_ha *ast_append_ha(char *sense, char *stuff, struct ast_ha *path)
 {
-	struct ast_ha *ha = malloc(sizeof(struct ast_ha));
+	struct ast_ha *ha;
 	char *nm = "255.255.255.255";
 	char tmp[256];
 	struct ast_ha *prev = NULL;
 	struct ast_ha *ret;
 	int x, z;
-	unsigned int y;
+	unsigned int y;		
+
 	ret = path;
 	while (path) {
 		prev = path;
 		path = path->next;
 	}
-	if (ha) {
+	if ((ha = ast_malloc(sizeof(*ha)))) {
 		ast_copy_string(tmp, stuff, sizeof(tmp));
 		nm = strchr(tmp, '/');
 		if (!nm) {

Modified: trunk/app.c
URL: http://svn.digium.com/view/asterisk/trunk/app.c?rev=10066&r1=10065&r2=10066&view=diff
==============================================================================
--- trunk/app.c (original)
+++ trunk/app.c Tue Feb 14 16:28:01 2006
@@ -425,9 +425,7 @@
 			return -1;
 		}
 	}
-	lin = malloc(sizeof(struct linear_state));
-	if (lin) {
-		memset(lin, 0, sizeof(lin));
+	if ((lin = ast_calloc(1, sizeof(*lin)))) {
 		lin->fd = fd;
 		lin->allowoverride = allowoverride;
 		lin->autoclose = autoclose;
@@ -1155,10 +1153,7 @@
 	int fd;
 	time_t start;
 
-	s = alloca(strlen(path) + 10);
-	fs = alloca(strlen(path) + 20);
-
-	if (!fs || !s) {
+	if (!(s = alloca(strlen(path) + 10)) || !(fs = alloca(strlen(path) + 20))) {
 		ast_log(LOG_WARNING, "Out of memory!\n");
 		return AST_LOCK_FAILURE;
 	}
@@ -1188,8 +1183,7 @@
 int ast_unlock_path(const char *path)
 {
 	char *s;
-	s = alloca(strlen(path) + 10);
-	if (!s)
+	if (!(s = alloca(strlen(path) + 10)))
 		return -1;
 	snprintf(s, strlen(path) + 9, "%s/%s", path, ".lock");
 	ast_log(LOG_DEBUG, "Unlocked path '%s'\n", path);
@@ -1514,9 +1508,8 @@
 	if (fd < 0) {
 		ast_log(LOG_WARNING, "Cannot open file '%s' for reading: %s\n", filename, strerror(errno));
 		return NULL;
-	}
-	output=(char *)malloc(count);
-	if (output) {
+	}	
+	if ((output = ast_malloc(count))) {
 		res = read(fd, output, count - 1);
 		if (res == count - 1) {
 			output[res] = '\0';
@@ -1525,8 +1518,7 @@
 			free(output);
 			output = NULL;
 		}
-	} else 
-		ast_log(LOG_WARNING, "Out of memory!\n");
+	}
 	close(fd);
 	return output;
 }

Modified: trunk/asterisk.c
URL: http://svn.digium.com/view/asterisk/trunk/asterisk.c?rev=10066&r1=10065&r2=10066&view=diff
==============================================================================
--- trunk/asterisk.c (original)
+++ trunk/asterisk.c Tue Feb 14 16:28:01 2006
@@ -238,9 +238,8 @@
 	work = ast_strdupa(version);
 	work = ast_strip(ast_strip_quoted(work, "$", "$"));
 	version_length = strlen(work) + 1;
-
-	new = calloc(1, sizeof(*new) + version_length);
-	if (!new)
+	
+	if (!(new = ast_calloc(1, sizeof(*new) + version_length)))
 		return;
 
 	new->file = file;
@@ -357,11 +356,9 @@
 {
 	int res = -1;
 	struct ast_atexit *ae;
-	ast_unregister_atexit(func);
-	ae = malloc(sizeof(struct ast_atexit));
+	ast_unregister_atexit(func);	
 	AST_LIST_LOCK(&atexits);
-	if (ae) {
-		memset(ae, 0, sizeof(struct ast_atexit));
+	if ((ae = ast_calloc(1, sizeof(*ae)))) {
 		AST_LIST_INSERT_HEAD(&atexits, ae, list);
 		ae->func = func;
 		res = 0;
@@ -481,8 +478,8 @@
 	/* ARGUSED */
 {
 	if (replace) {
-		char *t = alloca(strlen(s) + 2);
-		if (t) {
+		char *t;
+		if ((t = alloca(strlen(s) + 2))) {
 			sprintf(t, "\r%s", s);
 			if (complete)
 				ast_network_puts(t);
@@ -1350,7 +1347,7 @@
 						}
 						break;
 					case 'd': /* date */
-						memset(&tm, 0, sizeof(struct tm));
+						memset(&tm, 0, sizeof(tm));
 						time(&ts);
 						if (localtime_r(&ts, &tm)) {
 							strftime(p, sizeof(prompt) - strlen(prompt), "%Y-%m-%d", &tm);
@@ -1407,7 +1404,7 @@
 						break;
 #endif
 					case 't': /* time */
-						memset(&tm, 0, sizeof(struct tm));
+						memset(&tm, 0, sizeof(tm));
 						time(&ts);
 						if (localtime_r(&ts, &tm)) {
 							strftime(p, sizeof(prompt) - strlen(prompt), "%H:%M:%S", &tm);
@@ -1467,7 +1464,9 @@
 			break;
 		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(char *)))) {
+				/* TODO: Handle memory allocation failure */
+			}
 		}
 
 		match_list[matches++] = strdup(retstr);
@@ -1476,8 +1475,11 @@
 	if (!match_list)
 		return (char **) NULL;
 
-	if (matches>= match_list_len)
-		match_list = realloc(match_list, (match_list_len + 1) * sizeof(char *));
+	if (matches >= match_list_len) {
+		if (!(match_list = ast_realloc(match_list, (match_list_len + 1) * sizeof(char *)))) {
+			/* TODO: Handle memory allocation failure */
+		}
+	}
 
 	match_list[matches] = (char *) NULL;
 
@@ -1578,9 +1580,8 @@
 		if (nummatches > 0) {
 			char *mbuf;
 			int mlen = 0, maxmbuf = 2048;
-			/* Start with a 2048 byte buffer */
-			mbuf = malloc(maxmbuf);
-			if (!mbuf)
+			/* Start with a 2048 byte buffer */			
+			if (!(mbuf = ast_malloc(maxmbuf)))
 				return (char *)(CC_ERROR);
 			snprintf(buf, sizeof(buf),"_COMMAND MATCHESARRAY \"%s\" \"%s\"", lf->buffer, ptr); 
 			fdprint(ast_consock, buf);
@@ -1589,9 +1590,8 @@
 			while (!strstr(mbuf, AST_CLI_COMPLETE_EOF) && res != -1) {
 				if (mlen + 1024 > maxmbuf) {
 					/* Every step increment buffer 1024 bytes */
-					maxmbuf += 1024;
-					mbuf = realloc(mbuf, maxmbuf);
-					if (!mbuf)
+					maxmbuf += 1024;					
+					if (!(mbuf = ast_realloc(mbuf, maxmbuf)))
 						return (char *)(CC_ERROR);
 				}
 				/* Only read 1024 bytes at a time */



More information about the asterisk-commits mailing list