Index: app.c =================================================================== --- app.c (revision 8749) +++ app.c (working copy) @@ -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; } Index: acl.c =================================================================== --- acl.c (revision 8749) +++ acl.c (working copy) @@ -113,10 +113,13 @@ /* 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) { Index: asterisk.c =================================================================== --- asterisk.c (revision 8749) +++ asterisk.c (working copy) @@ -234,9 +234,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; @@ -353,11 +352,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; @@ -477,8 +474,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); @@ -1346,7 +1343,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); @@ -1403,7 +1400,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); @@ -1463,7 +1460,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); @@ -1472,8 +1471,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; @@ -1574,9 +1576,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); @@ -1585,9 +1586,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 */