From efae38a82d88ff964b2d35ebf0895f4cea8ab322 Mon Sep 17 00:00:00 2001 From: Russell Bryant <russell@russellbryant.com> Date: Sat, 21 Jan 2006 22:09:06 +0000 Subject: [PATCH] - conversions to allocation wrappers - replace malloc/memset with ast_calloc - replace malloc/ast_copy_string with ast_strdup (based on patch from issue #6299) git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@8410 65c4cc65-6c06-0410-ace0-fbb531ad65f3 --- res/res_crypto.c | 5 +---- res/res_features.c | 44 +++++++++++++++-------------------------- res/res_indications.c | 34 +++++++++++--------------------- res/res_monitor.c | 18 +++++++---------- res/res_musiconhold.c | 46 ++++++++++++++----------------------------- res/res_osp.c | 5 +---- 6 files changed, 51 insertions(+), 101 deletions(-) diff --git a/res/res_crypto.c b/res/res_crypto.c index 048006f69e..aaf6cf809e 100644 --- a/res/res_crypto.c +++ b/res/res_crypto.c @@ -215,13 +215,10 @@ static struct ast_key *try_load_key (char *dir, char *fname, int ifd, int ofd, i /* 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 */ diff --git a/res/res_features.c b/res/res_features.c index 72b8ad97c5..c705b2310c 100644 --- a/res/res_features.c +++ b/res/res_features.c @@ -263,13 +263,10 @@ int ast_park_call(struct ast_channel *chan, struct ast_channel *peer, int timeou 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 @@ int ast_masq_park_call(struct ast_channel *rchan, struct ast_channel *peer, int 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 @@ static int builtin_atxfer(struct ast_channel *chan, struct ast_channel *peer, st 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 @@ static int builtin_atxfer(struct ast_channel *chan, struct ast_channel *peer, st } ast_bridge_call_thread_launch(tobj); } else { - ast_log(LOG_WARNING, "Out of memory!\n"); ast_hangup(xferchan); ast_hangup(newchan); } @@ -1214,10 +1207,7 @@ static struct ast_channel *ast_feature_request_and_dial(struct ast_channel *call *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); @@ -2088,21 +2078,19 @@ static int load_config(void) } { - 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) { - ast_log(LOG_NOTICE, "Malloc failed at feature mapping\n"); - free(tmp_val); - var = var->next; - continue; + if (!(feature = find_feature(var->name))) { + mallocd = 1; + + if (!(feature = ast_calloc(1, sizeof(*feature)))) { + 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); diff --git a/res/res_indications.c b/res/res_indications.c index 65ac3a9ba1..1188b05865 100644 --- a/res/res_indications.c +++ b/res/res_indications.c @@ -47,7 +47,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$") #include "asterisk/module.h" #include "asterisk/translate.h" #include "asterisk/indications.h" - +#include "asterisk/utils.h" /* Globals */ static const char dtext[] = "Indications Configuration"; @@ -94,13 +94,10 @@ static int handle_add_indication(int fd, int argc, char *argv[]) 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 @@ static int ind_load_module(void) 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 @@ static int ind_load_module(void) 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 @@ static int ind_load_module(void) 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 @@ static int ind_load_module(void) 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; } diff --git a/res/res_monitor.c b/res/res_monitor.c index 745f5cb45f..9c154efb6b 100644 --- a/res/res_monitor.c +++ b/res/res_monitor.c @@ -150,12 +150,10 @@ int ast_monitor_start( struct ast_channel *chan, const char *format_spec, } } - 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,8 @@ static int start_monitor_exec(struct ast_channel *chan, void *data) 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())) + return -1; ast_cdr_setuserfield(chan, tmp); } if (waitforbridge) { @@ -491,17 +489,15 @@ static int start_monitor_action(struct mansession *s, struct message *m) } 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_strdup(c->name))) { 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='-'; + if ((d = strchr(fname, '/'))) + *d = '-'; } if (ast_monitor_start(c, format, fname, 1)) { diff --git a/res/res_musiconhold.c b/res/res_musiconhold.c index ffbd41713a..7ab065a5c8 100644 --- a/res/res_musiconhold.c +++ b/res/res_musiconhold.c @@ -279,18 +279,17 @@ static void *moh_files_alloc(struct ast_channel *chan, void *params) { 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 mohclass *get_mohbyname(char *name) 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 @@ static void *moh_alloc(struct ast_channel *chan, void *params) 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,14 +903,8 @@ static struct mohclass *moh_class_malloc(void) { struct mohclass *class; - class = malloc(sizeof(struct mohclass)); - - if (!class) - return NULL; - - memset(class, 0, sizeof(struct mohclass)); - - class->format = AST_FORMAT_SLINEAR; + if ((class = ast_calloc(1, sizeof(*class)))) + class->format = AST_FORMAT_SLINEAR; return class; } @@ -937,10 +927,8 @@ static int load_moh_classes(int reload) 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 @@ static int load_moh_classes(int reload) 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 @@ static int load_moh_classes(int reload) 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; } diff --git a/res/res_osp.c b/res/res_osp.c index 1c00531aa9..eb92f4862a 100644 --- a/res/res_osp.c +++ b/res/res_osp.c @@ -124,12 +124,9 @@ static int osp_build(struct ast_config *cfg, char *cat) 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)); -- GitLab