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);
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]);
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)
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];
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;
}
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;
}
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);
}
{
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));
}
{
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;
}
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 */
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 {
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;
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;
}
char prefix[256];
DBT key, data;
char *keys, *values;
+ int values_len;
int res;
int pass;
struct ast_db_entry *last = NULL;
} 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);
{
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;
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);
{
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;
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;
}