From 1c7126664dbf14c1f1c565eb130616ac7046a162 Mon Sep 17 00:00:00 2001
From: "Kevin P. Fleming" <kpfleming@digium.com>
Date: Tue, 14 Feb 2006 22:28:01 +0000
Subject: [PATCH] more memory allocation wrapper conversion (issue #6365)

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@10066 65c4cc65-6c06-0410-ace0-fbb531ad65f3
---
 acl.c      | 16 ++++++++++------
 app.c      | 20 ++++++--------------
 asterisk.c | 40 ++++++++++++++++++++--------------------
 3 files changed, 36 insertions(+), 40 deletions(-)

diff --git a/acl.c b/acl.c
index 6061d54c47..2b4e384715 100644
--- a/acl.c
+++ b/acl.c
@@ -113,9 +113,12 @@ static void ast_copy_ha(struct ast_ha *from, struct ast_ha *to)
 /* 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_duplicate_ha_list(struct ast_ha *original)
 
 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) {
diff --git a/app.c b/app.c
index fedc95d572..1d85fea321 100644
--- a/app.c
+++ b/app.c
@@ -425,9 +425,7 @@ int ast_linear_stream(struct ast_channel *chan, const char *filename, int fd, in
 			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 @@ enum AST_LOCK_RESULT ast_lock_path(const char *path)
 	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 @@ enum AST_LOCK_RESULT ast_lock_path(const char *path)
 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 @@ char *ast_read_textfile(const char *filename)
 	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 @@ char *ast_read_textfile(const char *filename)
 			free(output);
 			output = NULL;
 		}
-	} else 
-		ast_log(LOG_WARNING, "Out of memory!\n");
+	}
 	close(fd);
 	return output;
 }
diff --git a/asterisk.c b/asterisk.c
index d2d493d80b..bdf27e2676 100644
--- a/asterisk.c
+++ b/asterisk.c
@@ -238,9 +238,8 @@ void ast_register_file_version(const char *file, const char *version)
 	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 ast_register_atexit(void (*func)(void))
 {
 	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 @@ static void network_verboser(const char *s, int pos, int replace, int complete)
 	/* 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 @@ static char *cli_prompt(EditLine *el)
 						}
 						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 @@ static char *cli_prompt(EditLine *el)
 						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 @@ static char **ast_el_strtoarr(char *buf)
 			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 @@ static char **ast_el_strtoarr(char *buf)
 	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 @@ static char *cli_complete(EditLine *el, int ch)
 		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 @@ static char *cli_complete(EditLine *el, int ch)
 			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 */
-- 
GitLab