conversions to memory allocation wrappers, remove duplicated error messages,

remove unnecessary casts, malloc+memset to calloc (issue #6395)


git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@9310 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
Russell Bryant
2006-02-09 16:59:50 +00:00
parent 091a14754d
commit b96af335d8
5 changed files with 49 additions and 75 deletions

34
cli.c
View File

@@ -63,14 +63,14 @@ extern unsigned long global_fin, global_fout;
void ast_cli(int fd, char *fmt, ...) void ast_cli(int fd, char *fmt, ...)
{ {
char *stuff; char *stuff;
int res = 0; int res;
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
res = vasprintf(&stuff, fmt, ap); res = vasprintf(&stuff, fmt, ap);
va_end(ap); va_end(ap);
if (res == -1) { if (res == -1) {
ast_log(LOG_ERROR, "Out of memory\n"); ast_log(LOG_ERROR, "Memory allocation failure\n");
} else { } else {
ast_carefulwrite(fd, stuff, strlen(stuff), 100); ast_carefulwrite(fd, stuff, strlen(stuff), 100);
free(stuff); free(stuff);
@@ -520,8 +520,7 @@ static int handle_commandmatchesarray(int fd, int argc, char *argv[])
if (argc != 4) if (argc != 4)
return RESULT_SHOWUSAGE; return RESULT_SHOWUSAGE;
buf = malloc(buflen); if (!(buf = ast_malloc(buflen)))
if (!buf)
return RESULT_FAILURE; return RESULT_FAILURE;
buf[len] = '\0'; buf[len] = '\0';
matches = ast_cli_completion_matches(argv[2], argv[3]); 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) { if (len + matchlen >= buflen) {
buflen += matchlen * 3; buflen += matchlen * 3;
obuf = buf; obuf = buf;
buf = realloc(obuf, buflen); if (!(buf = ast_realloc(obuf, buflen)))
if (!buf) /* Memory allocation failure... Just free old buffer and be done */
/* Out of memory... Just free old buffer and be done */
free(obuf); free(obuf);
} }
if (buf) 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) { while ((retstr = ast_cli_generator(text, word, matches)) != NULL) {
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(*match_list))))
return NULL;
} }
match_list[++matches] = retstr; match_list[++matches] = retstr;
} }
if (!match_list) if (!match_list)
return (char **) NULL; return NULL;
which = 2; which = 2;
prevstr = match_list[1]; prevstr = match_list[1];
@@ -1291,14 +1290,18 @@ char **ast_cli_completion_matches(const char *text, const char *word)
max_equal = i; max_equal = i;
} }
retstr = malloc(max_equal + 1); if (!(retstr = ast_malloc(max_equal + 1)))
return NULL;
strncpy(retstr, match_list[1], max_equal); strncpy(retstr, match_list[1], max_equal);
retstr[max_equal] = '\0'; retstr[max_equal] = '\0';
match_list[0] = retstr; match_list[0] = retstr;
if (matches + 1 >= match_list_len) if (matches + 1 >= 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(*match_list))))
match_list[matches + 1] = (char *) NULL; return NULL;
}
match_list[matches + 1] = NULL;
return match_list; return match_list;
} }
@@ -1392,9 +1395,8 @@ int ast_cli_command(int fd, const char *s)
char *dup; char *dup;
int tws; int tws;
dup = parse_args(s, &x, argv, sizeof(argv) / sizeof(argv[0]), &tws); if (!(dup = parse_args(s, &x, argv, sizeof(argv) / sizeof(argv[0]), &tws))) {
if (!dup) { ast_log(LOG_ERROR, "Memory allocation failure\n");
ast_log(LOG_ERROR, "Out of Memory!\n");
return -1; return -1;
} }

View File

@@ -101,13 +101,11 @@ struct ast_config {
struct ast_variable *ast_variable_new(const char *name, const char *value) struct ast_variable *ast_variable_new(const char *name, const char *value)
{ {
struct ast_variable *variable; struct ast_variable *variable;
int name_len = strlen(name) + 1;
int length = strlen(name) + strlen(value) + 2 + sizeof(struct ast_variable); if ((variable = ast_calloc(1, name_len + strlen(value) + 1 + sizeof(*variable)))) {
variable = malloc(length);
if (variable) {
memset(variable, 0, length);
variable->name = variable->stuff; variable->name = variable->stuff;
variable->value = variable->stuff + strlen(name) + 1; variable->value = variable->stuff + name_len;
strcpy(variable->name,name); strcpy(variable->name,name);
strcpy(variable->value,value); strcpy(variable->value,value);
} }
@@ -203,9 +201,7 @@ struct ast_category *ast_category_new(const char *name)
{ {
struct ast_category *category; struct ast_category *category;
category = malloc(sizeof(struct ast_category)); if ((category = ast_calloc(1, sizeof(*category)))) {
if (category) {
memset(category, 0, sizeof(struct ast_category));
ast_copy_string(category->name, name, sizeof(category->name)); ast_copy_string(category->name, name, sizeof(category->name));
} }
@@ -329,9 +325,7 @@ struct ast_config *ast_config_new(void)
{ {
struct ast_config *config; struct ast_config *config;
config = malloc(sizeof(*config)); if ((config = ast_calloc(1, sizeof(*config)))) {
if (config) {
memset(config, 0, sizeof(*config));
config->max_include_level = MAX_INCLUDE_LEVEL; 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++ != '(') if (*c++ != '(')
c = NULL; c = NULL;
catname = cur; catname = cur;
*cat = newcat = ast_category_new(catname); if (!(*cat = newcat = ast_category_new(catname))) {
if (!newcat) {
ast_log(LOG_WARNING, "Out of memory, line %d of %s\n", lineno, configfile);
return -1; return -1;
} }
/* If there are options or categories to inherit from, process them now */ /* 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++; c++;
} else } else
object = 0; object = 0;
v = ast_variable_new(ast_strip(cur), ast_strip(c)); if ((v = ast_variable_new(ast_strip(cur), ast_strip(c)))) {
if (v) {
v->lineno = lineno; v->lineno = lineno;
v->object = object; v->object = object;
/* Put and reset comments */ /* Put and reset comments */
v->blanklines = 0; v->blanklines = 0;
ast_variable_append(*cat, v); ast_variable_append(*cat, v);
} else { } else {
ast_log(LOG_WARNING, "Out of memory, line %d\n", lineno);
return -1; return -1;
} }
} else { } else {
@@ -767,12 +757,10 @@ static int append_mapping(char *name, char *driver, char *database, char *table)
length += strlen(database) + 1; length += strlen(database) + 1;
if (table) if (table)
length += strlen(table) + 1; length += strlen(table) + 1;
map = malloc(length);
if (!map) if (!(map = ast_calloc(1, length)))
return -1; return -1;
memset(map, 0, length);
map->name = map->stuff; map->name = map->stuff;
strcpy(map->name, name); strcpy(map->name, name);
map->driver = map->name + strlen(map->name) + 1; map->driver = map->name + strlen(map->name) + 1;

18
db.c
View File

@@ -63,14 +63,11 @@ AST_MUTEX_DEFINE_STATIC(dblock);
static int dbinit(void) static int dbinit(void)
{ {
if (!astdb) { if (!astdb && !(astdb = dbopen((char *)ast_config_AST_DB, O_CREAT | O_RDWR, 0664, DB_BTREE, NULL))) {
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"); ast_log(LOG_WARNING, "Unable to open Asterisk database\n");
}
}
if (astdb)
return 0;
return -1; return -1;
}
return 0;
} }
@@ -402,6 +399,7 @@ struct ast_db_entry *ast_db_gettree(const char *family, const char *keytree)
char prefix[256]; char prefix[256];
DBT key, data; DBT key, data;
char *keys, *values; char *keys, *values;
int values_len;
int res; int res;
int pass; int pass;
struct ast_db_entry *last = NULL; struct ast_db_entry *last = NULL;
@@ -440,11 +438,10 @@ struct ast_db_entry *ast_db_gettree(const char *family, const char *keytree)
} else { } else {
values = "<bad value>"; values = "<bad value>";
} }
if (keymatch(keys, prefix)) { values_len = strlen(values) + 1;
cur = malloc(sizeof(struct ast_db_entry) + strlen(keys) + strlen(values) + 2); if (keymatch(keys, prefix) && (cur = ast_malloc(sizeof(*cur) + strlen(keys) + 1 + values_len))) {
if (cur) {
cur->next = NULL; cur->next = NULL;
cur->key = cur->data + strlen(values) + 1; cur->key = cur->data + values_len;
strcpy(cur->data, values); strcpy(cur->data, values);
strcpy(cur->key, keys); strcpy(cur->key, keys);
if (last) { if (last) {
@@ -455,7 +452,6 @@ struct ast_db_entry *ast_db_gettree(const char *family, const char *keytree)
last = cur; last = cur;
} }
} }
}
ast_mutex_unlock(&dblock); ast_mutex_unlock(&dblock);
return ret; return ret;
} }

View File

@@ -143,11 +143,7 @@ int ast_devstate_add(ast_devstate_cb_type callback, void *data)
{ {
struct devstate_cb *devcb; 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; return -1;
devcb->data = data; devcb->data = data;
@@ -198,16 +194,13 @@ static void do_state_change(const char *device)
static int __ast_device_state_changed_literal(char *buf) static int __ast_device_state_changed_literal(char *buf)
{ {
char *device, *tmp; char *device, *tmp;
struct state_change *change = NULL; struct state_change *change;
device = buf; device = buf;
tmp = strrchr(device, '-'); if ((tmp = strrchr(device, '-')))
if (tmp)
*tmp = '\0'; *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 */ /* we could not allocate a change struct, or */
/* there is no background thread, so process the change now */ /* there is no background thread, so process the change now */
do_state_change(device); do_state_change(device);

View File

@@ -83,11 +83,7 @@ struct ast_dnsmgr_entry *ast_dnsmgr_get(const char *name, struct in_addr *result
{ {
struct ast_dnsmgr_entry *entry; 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; return NULL;
entry->result = result; entry->result = result;
@@ -285,8 +281,7 @@ static struct ast_cli_entry cli_status = {
int dnsmgr_init(void) int dnsmgr_init(void)
{ {
sched = sched_context_create(); if (!(sched = sched_context_create())) {
if (!sched) {
ast_log(LOG_ERROR, "Unable to create schedule context.\n"); ast_log(LOG_ERROR, "Unable to create schedule context.\n");
return -1; return -1;
} }