more memory allocation wrapper conversion (issue #6365)

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@10066 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
Kevin P. Fleming
2006-02-14 22:28:01 +00:00
parent 5e6019c61c
commit 1c7126664d
3 changed files with 36 additions and 40 deletions

16
acl.c
View File

@@ -113,9 +113,12 @@ static void ast_copy_ha(struct ast_ha *from, struct ast_ha *to)
/* Create duplicate of ha structure */ /* Create duplicate of ha structure */
static struct ast_ha *ast_duplicate_ha(struct ast_ha *original) static struct ast_ha *ast_duplicate_ha(struct ast_ha *original)
{ {
struct ast_ha *new_ha = malloc(sizeof(struct ast_ha)); struct ast_ha *new_ha;
/* Copy from original to new object */
ast_copy_ha(original, 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; 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 *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 *nm = "255.255.255.255";
char tmp[256]; char tmp[256];
struct ast_ha *prev = NULL; struct ast_ha *prev = NULL;
struct ast_ha *ret; struct ast_ha *ret;
int x, z; int x, z;
unsigned int y; unsigned int y;
ret = path; ret = path;
while (path) { while (path) {
prev = path; prev = path;
path = path->next; path = path->next;
} }
if (ha) { if ((ha = ast_malloc(sizeof(*ha)))) {
ast_copy_string(tmp, stuff, sizeof(tmp)); ast_copy_string(tmp, stuff, sizeof(tmp));
nm = strchr(tmp, '/'); nm = strchr(tmp, '/');
if (!nm) { if (!nm) {

20
app.c
View File

@@ -425,9 +425,7 @@ int ast_linear_stream(struct ast_channel *chan, const char *filename, int fd, in
return -1; return -1;
} }
} }
lin = malloc(sizeof(struct linear_state)); if ((lin = ast_calloc(1, sizeof(*lin)))) {
if (lin) {
memset(lin, 0, sizeof(lin));
lin->fd = fd; lin->fd = fd;
lin->allowoverride = allowoverride; lin->allowoverride = allowoverride;
lin->autoclose = autoclose; lin->autoclose = autoclose;
@@ -1155,10 +1153,7 @@ enum AST_LOCK_RESULT ast_lock_path(const char *path)
int fd; int fd;
time_t start; time_t start;
s = alloca(strlen(path) + 10); if (!(s = alloca(strlen(path) + 10)) || !(fs = alloca(strlen(path) + 20))) {
fs = alloca(strlen(path) + 20);
if (!fs || !s) {
ast_log(LOG_WARNING, "Out of memory!\n"); ast_log(LOG_WARNING, "Out of memory!\n");
return AST_LOCK_FAILURE; 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) int ast_unlock_path(const char *path)
{ {
char *s; char *s;
s = alloca(strlen(path) + 10); if (!(s = alloca(strlen(path) + 10)))
if (!s)
return -1; return -1;
snprintf(s, strlen(path) + 9, "%s/%s", path, ".lock"); snprintf(s, strlen(path) + 9, "%s/%s", path, ".lock");
ast_log(LOG_DEBUG, "Unlocked path '%s'\n", path); ast_log(LOG_DEBUG, "Unlocked path '%s'\n", path);
@@ -1514,9 +1508,8 @@ char *ast_read_textfile(const char *filename)
if (fd < 0) { if (fd < 0) {
ast_log(LOG_WARNING, "Cannot open file '%s' for reading: %s\n", filename, strerror(errno)); ast_log(LOG_WARNING, "Cannot open file '%s' for reading: %s\n", filename, strerror(errno));
return NULL; return NULL;
} }
output=(char *)malloc(count); if ((output = ast_malloc(count))) {
if (output) {
res = read(fd, output, count - 1); res = read(fd, output, count - 1);
if (res == count - 1) { if (res == count - 1) {
output[res] = '\0'; output[res] = '\0';
@@ -1525,8 +1518,7 @@ char *ast_read_textfile(const char *filename)
free(output); free(output);
output = NULL; output = NULL;
} }
} else }
ast_log(LOG_WARNING, "Out of memory!\n");
close(fd); close(fd);
return output; return output;
} }

View File

@@ -238,9 +238,8 @@ void ast_register_file_version(const char *file, const char *version)
work = ast_strdupa(version); work = ast_strdupa(version);
work = ast_strip(ast_strip_quoted(work, "$", "$")); work = ast_strip(ast_strip_quoted(work, "$", "$"));
version_length = strlen(work) + 1; version_length = strlen(work) + 1;
new = calloc(1, sizeof(*new) + version_length); if (!(new = ast_calloc(1, sizeof(*new) + version_length)))
if (!new)
return; return;
new->file = file; new->file = file;
@@ -357,11 +356,9 @@ int ast_register_atexit(void (*func)(void))
{ {
int res = -1; int res = -1;
struct ast_atexit *ae; struct ast_atexit *ae;
ast_unregister_atexit(func); ast_unregister_atexit(func);
ae = malloc(sizeof(struct ast_atexit));
AST_LIST_LOCK(&atexits); AST_LIST_LOCK(&atexits);
if (ae) { if ((ae = ast_calloc(1, sizeof(*ae)))) {
memset(ae, 0, sizeof(struct ast_atexit));
AST_LIST_INSERT_HEAD(&atexits, ae, list); AST_LIST_INSERT_HEAD(&atexits, ae, list);
ae->func = func; ae->func = func;
res = 0; res = 0;
@@ -481,8 +478,8 @@ static void network_verboser(const char *s, int pos, int replace, int complete)
/* ARGUSED */ /* ARGUSED */
{ {
if (replace) { if (replace) {
char *t = alloca(strlen(s) + 2); char *t;
if (t) { if ((t = alloca(strlen(s) + 2))) {
sprintf(t, "\r%s", s); sprintf(t, "\r%s", s);
if (complete) if (complete)
ast_network_puts(t); ast_network_puts(t);
@@ -1350,7 +1347,7 @@ static char *cli_prompt(EditLine *el)
} }
break; break;
case 'd': /* date */ case 'd': /* date */
memset(&tm, 0, sizeof(struct tm)); memset(&tm, 0, sizeof(tm));
time(&ts); time(&ts);
if (localtime_r(&ts, &tm)) { if (localtime_r(&ts, &tm)) {
strftime(p, sizeof(prompt) - strlen(prompt), "%Y-%m-%d", &tm); strftime(p, sizeof(prompt) - strlen(prompt), "%Y-%m-%d", &tm);
@@ -1407,7 +1404,7 @@ static char *cli_prompt(EditLine *el)
break; break;
#endif #endif
case 't': /* time */ case 't': /* time */
memset(&tm, 0, sizeof(struct tm)); memset(&tm, 0, sizeof(tm));
time(&ts); time(&ts);
if (localtime_r(&ts, &tm)) { if (localtime_r(&ts, &tm)) {
strftime(p, sizeof(prompt) - strlen(prompt), "%H:%M:%S", &tm); strftime(p, sizeof(prompt) - strlen(prompt), "%H:%M:%S", &tm);
@@ -1467,7 +1464,9 @@ static char **ast_el_strtoarr(char *buf)
break; break;
if (matches + 1 >= match_list_len) { if (matches + 1 >= match_list_len) {
match_list_len <<= 1; 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); match_list[matches++] = strdup(retstr);
@@ -1476,8 +1475,11 @@ static char **ast_el_strtoarr(char *buf)
if (!match_list) if (!match_list)
return (char **) NULL; return (char **) NULL;
if (matches>= match_list_len) if (matches >= match_list_len) {
match_list = realloc(match_list, (match_list_len + 1) * sizeof(char *)); if (!(match_list = ast_realloc(match_list, (match_list_len + 1) * sizeof(char *)))) {
/* TODO: Handle memory allocation failure */
}
}
match_list[matches] = (char *) NULL; match_list[matches] = (char *) NULL;
@@ -1578,9 +1580,8 @@ static char *cli_complete(EditLine *el, int ch)
if (nummatches > 0) { if (nummatches > 0) {
char *mbuf; char *mbuf;
int mlen = 0, maxmbuf = 2048; int mlen = 0, maxmbuf = 2048;
/* Start with a 2048 byte buffer */ /* Start with a 2048 byte buffer */
mbuf = malloc(maxmbuf); if (!(mbuf = ast_malloc(maxmbuf)))
if (!mbuf)
return (char *)(CC_ERROR); return (char *)(CC_ERROR);
snprintf(buf, sizeof(buf),"_COMMAND MATCHESARRAY \"%s\" \"%s\"", lf->buffer, ptr); snprintf(buf, sizeof(buf),"_COMMAND MATCHESARRAY \"%s\" \"%s\"", lf->buffer, ptr);
fdprint(ast_consock, buf); 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) { while (!strstr(mbuf, AST_CLI_COMPLETE_EOF) && res != -1) {
if (mlen + 1024 > maxmbuf) { if (mlen + 1024 > maxmbuf) {
/* Every step increment buffer 1024 bytes */ /* Every step increment buffer 1024 bytes */
maxmbuf += 1024; maxmbuf += 1024;
mbuf = realloc(mbuf, maxmbuf); if (!(mbuf = ast_realloc(mbuf, maxmbuf)))
if (!mbuf)
return (char *)(CC_ERROR); return (char *)(CC_ERROR);
} }
/* Only read 1024 bytes at a time */ /* Only read 1024 bytes at a time */