Index: res/res_monitor.c =================================================================== --- res/res_monitor.c (revision 8275) +++ res/res_monitor.c (working copy) @@ -150,12 +150,10 @@ } } - monitor = malloc(sizeof(struct ast_channel_monitor)); - if (!monitor) { + if (!(monitor = ast_calloc(1, sizeof(*monitor)))) { UNLOCK_IF_NEEDED(&chan->lock, need_lock); return -1; } - memset(monitor, 0, sizeof(struct ast_channel_monitor)); /* Determine file names */ if (!ast_strlen_zero(fname_base)) { @@ -416,8 +414,9 @@ if (urlprefix) { snprintf(tmp,sizeof(tmp) - 1,"%s/%s.%s",urlprefix,fname_base, ((strcmp(format,"gsm")) ? "wav" : "gsm")); - if (!chan->cdr) - chan->cdr = ast_cdr_alloc(); + if (!chan->cdr && !(chan->cdr = ast_cdr_alloc())) { + /* TODO: Handle memory allocation failure */ + } ast_cdr_setuserfield(chan, tmp); } if (waitforbridge) { @@ -491,14 +490,12 @@ } if (ast_strlen_zero(fname)) { - /* No filename base specified, default to channel name as per CLI */ - fname = malloc (FILENAME_MAX); - if (!fname) { + /* No filename base specified, default to channel name as per CLI */ + if (!(fname = ast_malloc(FILENAME_MAX))) { astman_send_error(s, m, "Could not start monitoring channel"); ast_mutex_unlock(&c->lock); return 0; } - memset(fname, 0, FILENAME_MAX); ast_copy_string(fname, c->name, FILENAME_MAX); /* Channels have the format technology/channel_name - have to replace that / */ if ((d=strchr(fname, '/'))) *d='-'; Index: res/res_odbc.c =================================================================== --- res/res_odbc.c (revision 8275) +++ res/res_odbc.c (working copy) @@ -49,6 +49,8 @@ #include "asterisk/cli.h" #include "asterisk/lock.h" #include "asterisk/res_odbc.h" +#include "asterisk/utils.h" + #define MAX_ODBC_HANDLES 25 struct odbc_list @@ -102,7 +104,7 @@ { int x = 0; for (x = 0; x < MAX_ODBC_HANDLES; x++) { - memset(&ODBC_REGISTRY[x], 0, sizeof(struct odbc_list)); + memset(&ODBC_REGISTRY[x], 0, sizeof(*ODBC_REGISTRY)); } } @@ -260,9 +262,8 @@ if (config) { for (cat = ast_category_browse(config, NULL); cat; cat=ast_category_browse(config, cat)) { if (!strcmp(cat, "ENV")) { - for (v = ast_variable_browse(config, cat); v; v = v->next) { - env_var = malloc(strlen(v->name) + strlen(v->value) + 2); - if (env_var) { + for (v = ast_variable_browse(config, cat); v; v = v->next) { + if ((env_var = ast_malloc(strlen(v->name) + strlen(v->value) + 2))) { sprintf(env_var, "%s=%s", v->name, v->value); ast_log(LOG_NOTICE, "Adding ENV var: %s=%s\n", v->name, v->value); putenv(env_var); @@ -429,31 +430,25 @@ odbc_obj *new_odbc_obj(char *name, char *dsn, char *username, char *password) { static odbc_obj *new; - - new = malloc(sizeof(odbc_obj)); - if (!new) + + if (!(new = ast_calloc(1, sizeof(*new)))) return NULL; - memset(new, 0, sizeof(odbc_obj)); new->env = SQL_NULL_HANDLE; - new->name = malloc(strlen(name) + 1); - if (new->name == NULL) + if (!(new->name = ast_malloc(strlen(name) + 1))) return NULL; - new->dsn = malloc(strlen(dsn) + 1); - if (new->dsn == NULL) + if (!(new->dsn = ast_malloc(strlen(dsn) + 1))) return NULL; if (username) { - new->username = malloc(strlen(username) + 1); - if (new->username == NULL) + if (!(new->username = ast_malloc(strlen(username) + 1))) return NULL; strcpy(new->username, username); } if (password) { - new->password = malloc(strlen(password) + 1); - if (new->password == NULL) + if (!(new->password = ast_malloc(strlen(password) + 1))) return NULL; strcpy(new->password, password); } Index: res/res_features.c =================================================================== --- res/res_features.c (revision 8275) +++ res/res_features.c (working copy) @@ -263,13 +263,10 @@ int i,x,parking_range; char exten[AST_MAX_EXTENSION]; struct ast_context *con; - - pu = malloc(sizeof(struct parkeduser)); - if (!pu) { - ast_log(LOG_WARNING, "Out of memory\n"); + + if (!(pu = ast_calloc(1, sizeof(*pu)))) { return -1; } - memset(pu, 0, sizeof(struct parkeduser)); ast_mutex_lock(&parking_lock); parking_range = parking_stop - parking_start+1; for (i = 0; i < parking_range; i++) { @@ -386,8 +383,7 @@ struct ast_frame *f; /* Make a new, fake channel that we'll use to masquerade in the real one */ - chan = ast_channel_alloc(0); - if (chan) { + if ((chan = ast_channel_alloc(0))) { /* Let us keep track of the channel name */ snprintf(chan->name, sizeof (chan->name), "Parked/%s",rchan->name); @@ -785,10 +781,8 @@ newchan->_state = AST_STATE_UP; ast_clear_flag(newchan, AST_FLAGS_ALL); newchan->_softhangup = 0; - - tobj = malloc(sizeof(struct ast_bridge_thread_obj)); - if (tobj) { - memset(tobj,0,sizeof(struct ast_bridge_thread_obj)); + + if ((tobj = ast_calloc(1, sizeof(*tobj)))) { tobj->chan = xferchan; tobj->peer = newchan; tobj->bconfig = *config; @@ -800,7 +794,6 @@ } ast_bridge_call_thread_launch(tobj); } else { - ast_log(LOG_WARNING, "Out of memory!\n"); ast_hangup(xferchan); ast_hangup(newchan); } @@ -1213,10 +1206,7 @@ *outstate = state; if (chan && res <= 0) { - if (!chan->cdr) { - chan->cdr = ast_cdr_alloc(); - } - if (chan->cdr) { + if (chan->cdr || (chan->cdr = ast_cdr_alloc())) { char tmp[256]; ast_cdr_init(chan->cdr, chan); snprintf(tmp, 256, "%s/%s", type, (char *)data); @@ -2087,21 +2077,19 @@ } { - struct ast_call_feature *feature=find_feature(var->name); - int mallocd=0; + struct ast_call_feature *feature; + int mallocd = 0; - if (!feature) { - feature=malloc(sizeof(struct ast_call_feature)); - mallocd=1; + if (!(feature = find_feature(var->name))) { + mallocd = 1; + + if (!(feature = ast_calloc(1, sizeof(*feature)))) { + free(tmp_val); + var = var->next; + continue; + } } - if (!feature) { - ast_log(LOG_NOTICE, "Malloc failed at feature mapping\n"); - free(tmp_val); - var = var->next; - continue; - } - memset(feature,0,sizeof(struct ast_call_feature)); ast_copy_string(feature->sname,var->name,FEATURE_SNAME_LEN); ast_copy_string(feature->app,app,FEATURE_APP_LEN); ast_copy_string(feature->exten, exten,FEATURE_EXTEN_LEN); Index: res/res_crypto.c =================================================================== --- res/res_crypto.c (revision 8275) +++ res/res_crypto.c (working copy) @@ -215,13 +215,10 @@ /* Make fname just be the normal name now */ *c = '\0'; if (!key) { - key = (struct ast_key *)malloc(sizeof(struct ast_key)); - if (!key) { - ast_log(LOG_WARNING, "Out of memory\n"); + if (!(key = ast_calloc(1, sizeof(*key)))) { fclose(f); return NULL; } - memset(key, 0, sizeof(struct ast_key)); } /* At this point we have a key structure (old or new). Time to fill it with what we know */ Index: res/res_osp.c =================================================================== --- res/res_osp.c (revision 8275) +++ res/res_osp.c (working copy) @@ -124,12 +124,9 @@ ast_mutex_unlock(&osplock); if (!osp) { mallocd = 1; - osp = malloc(sizeof(struct osp_provider)); - if (!osp) { - ast_log(LOG_WARNING, "Out of memory!\n"); + if (!(osp = ast_calloc(1, sizeof(*osp)))) { return -1; } - memset(osp, 0, sizeof(struct osp_provider)); osp->handle = -1; } ast_copy_string(osp->name, cat, sizeof(osp->name)); Index: res/res_indications.c =================================================================== --- res/res_indications.c (revision 8275) +++ res/res_indications.c (working copy) @@ -47,8 +47,8 @@ #include "asterisk/module.h" #include "asterisk/translate.h" #include "asterisk/indications.h" +#include "asterisk/utils.h" - /* Globals */ static const char dtext[] = "Indications Configuration"; static const char config[] = "indications.conf"; @@ -94,13 +94,10 @@ if (!tz) { /* country does not exist, create it */ ast_log(LOG_NOTICE, "Country '%s' does not exist, creating it.\n",argv[2]); - - tz = malloc(sizeof(struct tone_zone)); - if (!tz) { - ast_log(LOG_WARNING, "Out of memory\n"); + + if (!(tz = ast_calloc(1, sizeof(*tz)))) { return -1; } - memset(tz,0,sizeof(struct tone_zone)); ast_copy_string(tz->country,argv[2],sizeof(tz->country)); if (ast_register_indication_country(tz)) { ast_log(LOG_WARNING, "Unable to register new country\n"); @@ -257,14 +254,11 @@ if (!strcasecmp(cxt, "general")) { cxt = ast_category_browse(cfg, cxt); continue; - } - tones = malloc(sizeof(struct tone_zone)); - if (!tones) { - ast_log(LOG_WARNING,"Out of memory\n"); + } + if (!(tones = ast_calloc(1, sizeof(*tones)))) { ast_config_destroy(cfg); return -1; } - memset(tones,0,sizeof(struct tone_zone)); ast_copy_string(tones->country,cxt,sizeof(tones->country)); v = ast_variable_browse(cfg, cxt); @@ -281,10 +275,8 @@ ast_log(LOG_WARNING,"Invalid ringcadence given '%s' at line %d.\n",ring,v->lineno); ring = strsep(&c,","); continue; - } - tmp = realloc(tones->ringcadence,(tones->nrringcadence+1)*sizeof(int)); - if (!tmp) { - ast_log(LOG_WARNING, "Out of memory\n"); + } + if (!(tmp = ast_realloc(tones->ringcadence, (tones->nrringcadence + 1) * sizeof(int)))) { ast_config_destroy(cfg); return -1; } @@ -299,13 +291,11 @@ c = countries; country = strsep(&c,","); while (country) { - struct tone_zone* azone = malloc(sizeof(struct tone_zone)); - if (!azone) { - ast_log(LOG_WARNING,"Out of memory\n"); + struct tone_zone* azone; + if (!(azone = ast_calloc(1, sizeof(*azone)))) { ast_config_destroy(cfg); return -1; } - memset(azone,0,sizeof(struct tone_zone)); ast_copy_string(azone->country, country, sizeof(azone->country)); ast_copy_string(azone->alias, cxt, sizeof(azone->alias)); if (ast_register_indication_country(azone)) { @@ -325,10 +315,8 @@ goto out; } } - /* not there, add it to the back */ - ts = malloc(sizeof(struct tone_zone_sound)); - if (!ts) { - ast_log(LOG_WARNING, "Out of memory\n"); + /* not there, add it to the back */ + if (!(ts = ast_malloc(sizeof(*ts)))) { ast_config_destroy(cfg); return -1; } Index: res/res_musiconhold.c =================================================================== --- res/res_musiconhold.c (revision 8275) +++ res/res_musiconhold.c (working copy) @@ -279,18 +279,17 @@ { struct moh_files_state *state; struct mohclass *class = params; - int allocated = 0; - if (!chan->music_state && (state = malloc(sizeof(struct moh_files_state)))) { + if (!chan->music_state && (state = ast_calloc(1, sizeof(*state)))) { chan->music_state = state; - allocated = 1; + state->class = class; } else state = chan->music_state; if (state) { - if (allocated || state->class != class) { + if (state->class != class) { /* initialize */ - memset(state, 0, sizeof(struct moh_files_state)); + memset(state, 0, sizeof(*state)); state->class = class; } @@ -611,11 +610,9 @@ static struct mohdata *mohalloc(struct mohclass *cl) { struct mohdata *moh; - long flags; - moh = malloc(sizeof(struct mohdata)); - if (!moh) + long flags; + if (!(moh = ast_calloc(1, sizeof(*moh)))) return NULL; - memset(moh, 0, sizeof(struct mohdata)); if (pipe(moh->pipe)) { ast_log(LOG_WARNING, "Failed to create pipe: %s\n", strerror(errno)); free(moh); @@ -669,8 +666,7 @@ struct mohdata *res; struct mohclass *class = params; - res = mohalloc(class); - if (res) { + if ((res = mohalloc(class))) { res->origwfmt = chan->writeformat; if (ast_set_write_format(chan, class->format)) { ast_log(LOG_WARNING, "Unable to set channel '%s' to format '%s'\n", chan->name, ast_codec2str(class->format)); @@ -907,15 +903,9 @@ { struct mohclass *class; - class = malloc(sizeof(struct mohclass)); + if ((class = ast_calloc(1, sizeof(*class)))) + class->format = AST_FORMAT_SLINEAR; - if (!class) - return NULL; - - memset(class, 0, sizeof(struct mohclass)); - - class->format = AST_FORMAT_SLINEAR; - return class; } @@ -937,10 +927,8 @@ cat = ast_category_browse(cfg, NULL); for (; cat; cat = ast_category_browse(cfg, cat)) { - if (strcasecmp(cat, "classes") && strcasecmp(cat, "moh_files")) { - class = moh_class_malloc(); - if (!class) { - ast_log(LOG_WARNING, "Out of memory!\n"); + if (strcasecmp(cat, "classes") && strcasecmp(cat, "moh_files")) { + if (!(class = moh_class_malloc())) { break; } ast_copy_string(class->name, cat, sizeof(class->name)); @@ -1005,10 +993,8 @@ args = strchr(data, ','); if (args) *args++ = '\0'; - if (!(get_mohbyname(var->name))) { - class = moh_class_malloc(); - if (!class) { - ast_log(LOG_WARNING, "Out of memory!\n"); + if (!(get_mohbyname(var->name))) { + if (!(class = moh_class_malloc())) { return numclasses; } @@ -1033,10 +1019,8 @@ if (!(get_mohbyname(var->name))) { args = strchr(var->value, ','); if (args) - *args++ = '\0'; - class = moh_class_malloc(); - if (!class) { - ast_log(LOG_WARNING, "Out of memory!\n"); + *args++ = '\0'; + if (!(class = moh_class_malloc())) { return numclasses; }