Index: config.c =================================================================== --- config.c (revision 9039) +++ config.c (working copy) @@ -101,13 +101,11 @@ struct ast_variable *ast_variable_new(const char *name, const char *value) { struct ast_variable *variable; + int name_len = strlen(name) + 1; - int length = strlen(name) + strlen(value) + 2 + sizeof(struct ast_variable); - variable = malloc(length); - if (variable) { - memset(variable, 0, length); + 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; Index: db.c =================================================================== --- db.c (revision 9039) +++ db.c (working copy) @@ -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 && !(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; } - if (astdb) - return 0; - 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 = ""; } - 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); Index: devicestate.c =================================================================== --- devicestate.c (revision 9039) +++ devicestate.c (working copy) @@ -143,13 +143,9 @@ { struct devstate_cb *devcb; - if (!callback) + if (!callback || !(devcb = ast_calloc(1, sizeof(*devcb)))) return -1; - devcb = calloc(1, sizeof(*devcb)); - if (!devcb) - return -1; - devcb->data = data; devcb->callback = callback; @@ -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); Index: cli.c =================================================================== --- cli.c (revision 9039) +++ cli.c (working copy) @@ -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,7 +1272,9 @@ 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)))) { + /* TODO: Handle memory allocation failure */ + } } match_list[++matches] = retstr; } @@ -1291,13 +1291,18 @@ max_equal = i; } - retstr = malloc(max_equal + 1); + if (!(retstr = ast_malloc(max_equal + 1))) { + /* TODO: Handle memory allocation failure */ + } 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 *)); + if (matches + 1 >= match_list_len) { + if (!(match_list = ast_realloc(match_list, (match_list_len + 1) * sizeof(*match_list)))) { + /* TODO: Handle memory allocation failure */ + } + } match_list[matches + 1] = (char *) NULL; return match_list; @@ -1391,10 +1396,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; } Index: dnsmgr.c =================================================================== --- dnsmgr.c (revision 9039) +++ dnsmgr.c (working copy) @@ -83,13 +83,9 @@ { struct ast_dnsmgr_entry *entry; - if (!result || ast_strlen_zero(name)) + if (!result || ast_strlen_zero(name) || !(entry = ast_calloc(1, sizeof(*entry) + strlen(name)))) return NULL; - entry = calloc(1, sizeof(*entry) + strlen(name)); - if (!entry) - return NULL; - entry->result = result; strcpy(entry->name, name); @@ -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; }