diff options
-rw-r--r-- | cli.c | 36 | ||||
-rw-r--r-- | config.c | 28 | ||||
-rw-r--r-- | db.c | 36 | ||||
-rw-r--r-- | devicestate.c | 15 | ||||
-rw-r--r-- | dnsmgr.c | 9 |
5 files changed, 49 insertions, 75 deletions
@@ -63,14 +63,14 @@ extern unsigned long global_fin, global_fout; 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 @@ static int handle_commandmatchesarray(int fd, int argc, char *argv[]) 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 @@ static int handle_commandmatchesarray(int fd, int argc, char *argv[]) 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,13 +1272,14 @@ char **ast_cli_completion_matches(const char *text, const char *word) 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)))) + return NULL; } match_list[++matches] = retstr; } if (!match_list) - return (char **) NULL; + return NULL; which = 2; prevstr = match_list[1]; @@ -1291,14 +1290,18 @@ char **ast_cli_completion_matches(const char *text, const char *word) max_equal = i; } - retstr = malloc(max_equal + 1); + if (!(retstr = ast_malloc(max_equal + 1))) + return NULL; + 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 *)); - match_list[matches + 1] = (char *) NULL; + if (matches + 1 >= match_list_len) { + if (!(match_list = ast_realloc(match_list, (match_list_len + 1) * sizeof(*match_list)))) + return NULL; + } + match_list[matches + 1] = NULL; return match_list; } @@ -1391,10 +1394,9 @@ int ast_cli_command(int fd, const char *s) 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; } @@ -101,13 +101,11 @@ struct ast_config { 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 *ast_category_new(const char *name) { 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 *ast_config_new(void) { 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 @@ static int process_text_line(struct ast_config *cfg, struct ast_category **cat, 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 @@ static int process_text_line(struct ast_config *cfg, struct ast_category **cat, 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 @@ static int append_mapping(char *name, char *driver, char *database, char *table) 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; @@ -63,14 +63,11 @@ AST_MUTEX_DEFINE_STATIC(dblock); 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 @@ struct ast_db_entry *ast_db_gettree(const char *family, const char *keytree) 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 @@ struct ast_db_entry *ast_db_gettree(const char *family, const char *keytree) } else { values = "<bad value>"; } - 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); diff --git a/devicestate.c b/devicestate.c index d7f39612c..93200a9c0 100644 --- a/devicestate.c +++ b/devicestate.c @@ -143,11 +143,7 @@ int ast_devstate_add(ast_devstate_cb_type callback, void *data) { struct devstate_cb *devcb; - if (!callback) - return -1; - - devcb = calloc(1, sizeof(*devcb)); - if (!devcb) + if (!callback || !(devcb = ast_calloc(1, sizeof(*devcb)))) return -1; devcb->data = data; @@ -198,16 +194,13 @@ static void do_state_change(const char *device) 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); @@ -83,11 +83,7 @@ struct ast_dnsmgr_entry *ast_dnsmgr_get(const char *name, struct in_addr *result { struct ast_dnsmgr_entry *entry; - if (!result || ast_strlen_zero(name)) - return NULL; - - entry = calloc(1, sizeof(*entry) + strlen(name)); - if (!entry) + if (!result || ast_strlen_zero(name) || !(entry = ast_calloc(1, sizeof(*entry) + strlen(name)))) return NULL; entry->result = result; @@ -285,8 +281,7 @@ static struct ast_cli_entry cli_status = { 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; } |