- 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
1.4
Russell Bryant 20 years ago
parent 039ee896c7
commit efae38a82d

@ -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 */ /* Make fname just be the normal name now */
*c = '\0'; *c = '\0';
if (!key) { if (!key) {
key = (struct ast_key *)malloc(sizeof(struct ast_key)); if (!(key = ast_calloc(1, sizeof(*key)))) {
if (!key) {
ast_log(LOG_WARNING, "Out of memory\n");
fclose(f); fclose(f);
return NULL; return NULL;
} }
memset(key, 0, sizeof(struct ast_key));
} }
/* At this point we have a key structure (old or new). Time to /* At this point we have a key structure (old or new). Time to
fill it with what we know */ fill it with what we know */

@ -264,12 +264,9 @@ int ast_park_call(struct ast_channel *chan, struct ast_channel *peer, int timeou
char exten[AST_MAX_EXTENSION]; char exten[AST_MAX_EXTENSION];
struct ast_context *con; struct ast_context *con;
pu = malloc(sizeof(struct parkeduser)); if (!(pu = ast_calloc(1, sizeof(*pu)))) {
if (!pu) {
ast_log(LOG_WARNING, "Out of memory\n");
return -1; return -1;
} }
memset(pu, 0, sizeof(struct parkeduser));
ast_mutex_lock(&parking_lock); ast_mutex_lock(&parking_lock);
parking_range = parking_stop - parking_start+1; parking_range = parking_stop - parking_start+1;
for (i = 0; i < parking_range; i++) { 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; struct ast_frame *f;
/* Make a new, fake channel that we'll use to masquerade in the real one */ /* Make a new, fake channel that we'll use to masquerade in the real one */
chan = ast_channel_alloc(0); if ((chan = ast_channel_alloc(0))) {
if (chan) {
/* Let us keep track of the channel name */ /* Let us keep track of the channel name */
snprintf(chan->name, sizeof (chan->name), "Parked/%s",rchan->name); snprintf(chan->name, sizeof (chan->name), "Parked/%s",rchan->name);
@ -786,9 +782,7 @@ static int builtin_atxfer(struct ast_channel *chan, struct ast_channel *peer, st
ast_clear_flag(newchan, AST_FLAGS_ALL); ast_clear_flag(newchan, AST_FLAGS_ALL);
newchan->_softhangup = 0; newchan->_softhangup = 0;
tobj = malloc(sizeof(struct ast_bridge_thread_obj)); if ((tobj = ast_calloc(1, sizeof(*tobj)))) {
if (tobj) {
memset(tobj,0,sizeof(struct ast_bridge_thread_obj));
tobj->chan = xferchan; tobj->chan = xferchan;
tobj->peer = newchan; tobj->peer = newchan;
tobj->bconfig = *config; 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); ast_bridge_call_thread_launch(tobj);
} else { } else {
ast_log(LOG_WARNING, "Out of memory!\n");
ast_hangup(xferchan); ast_hangup(xferchan);
ast_hangup(newchan); ast_hangup(newchan);
} }
@ -1214,10 +1207,7 @@ static struct ast_channel *ast_feature_request_and_dial(struct ast_channel *call
*outstate = state; *outstate = state;
if (chan && res <= 0) { if (chan && res <= 0) {
if (!chan->cdr) { if (chan->cdr || (chan->cdr = ast_cdr_alloc())) {
chan->cdr = ast_cdr_alloc();
}
if (chan->cdr) {
char tmp[256]; char tmp[256];
ast_cdr_init(chan->cdr, chan); ast_cdr_init(chan->cdr, chan);
snprintf(tmp, 256, "%s/%s", type, (char *)data); 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); struct ast_call_feature *feature;
int mallocd=0; int mallocd = 0;
if (!feature) { if (!(feature = find_feature(var->name))) {
feature=malloc(sizeof(struct ast_call_feature)); mallocd = 1;
mallocd=1;
} if (!(feature = ast_calloc(1, sizeof(*feature)))) {
if (!feature) { free(tmp_val);
ast_log(LOG_NOTICE, "Malloc failed at feature mapping\n"); var = var->next;
free(tmp_val); continue;
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->sname,var->name,FEATURE_SNAME_LEN);
ast_copy_string(feature->app,app,FEATURE_APP_LEN); ast_copy_string(feature->app,app,FEATURE_APP_LEN);
ast_copy_string(feature->exten, exten,FEATURE_EXTEN_LEN); ast_copy_string(feature->exten, exten,FEATURE_EXTEN_LEN);

@ -47,7 +47,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#include "asterisk/module.h" #include "asterisk/module.h"
#include "asterisk/translate.h" #include "asterisk/translate.h"
#include "asterisk/indications.h" #include "asterisk/indications.h"
#include "asterisk/utils.h"
/* Globals */ /* Globals */
static const char dtext[] = "Indications Configuration"; static const char dtext[] = "Indications Configuration";
@ -95,12 +95,9 @@ static int handle_add_indication(int fd, int argc, char *argv[])
/* country does not exist, create it */ /* country does not exist, create it */
ast_log(LOG_NOTICE, "Country '%s' does not exist, creating it.\n",argv[2]); ast_log(LOG_NOTICE, "Country '%s' does not exist, creating it.\n",argv[2]);
tz = malloc(sizeof(struct tone_zone)); if (!(tz = ast_calloc(1, sizeof(*tz)))) {
if (!tz) {
ast_log(LOG_WARNING, "Out of memory\n");
return -1; return -1;
} }
memset(tz,0,sizeof(struct tone_zone));
ast_copy_string(tz->country,argv[2],sizeof(tz->country)); ast_copy_string(tz->country,argv[2],sizeof(tz->country));
if (ast_register_indication_country(tz)) { if (ast_register_indication_country(tz)) {
ast_log(LOG_WARNING, "Unable to register new country\n"); ast_log(LOG_WARNING, "Unable to register new country\n");
@ -258,13 +255,10 @@ static int ind_load_module(void)
cxt = ast_category_browse(cfg, cxt); cxt = ast_category_browse(cfg, cxt);
continue; continue;
} }
tones = malloc(sizeof(struct tone_zone)); if (!(tones = ast_calloc(1, sizeof(*tones)))) {
if (!tones) {
ast_log(LOG_WARNING,"Out of memory\n");
ast_config_destroy(cfg); ast_config_destroy(cfg);
return -1; return -1;
} }
memset(tones,0,sizeof(struct tone_zone));
ast_copy_string(tones->country,cxt,sizeof(tones->country)); ast_copy_string(tones->country,cxt,sizeof(tones->country));
v = ast_variable_browse(cfg, cxt); v = ast_variable_browse(cfg, cxt);
@ -282,9 +276,7 @@ static int ind_load_module(void)
ring = strsep(&c,","); ring = strsep(&c,",");
continue; continue;
} }
tmp = realloc(tones->ringcadence,(tones->nrringcadence+1)*sizeof(int)); if (!(tmp = ast_realloc(tones->ringcadence, (tones->nrringcadence + 1) * sizeof(int)))) {
if (!tmp) {
ast_log(LOG_WARNING, "Out of memory\n");
ast_config_destroy(cfg); ast_config_destroy(cfg);
return -1; return -1;
} }
@ -299,13 +291,11 @@ static int ind_load_module(void)
c = countries; c = countries;
country = strsep(&c,","); country = strsep(&c,",");
while (country) { while (country) {
struct tone_zone* azone = malloc(sizeof(struct tone_zone)); struct tone_zone* azone;
if (!azone) { if (!(azone = ast_calloc(1, sizeof(*azone)))) {
ast_log(LOG_WARNING,"Out of memory\n");
ast_config_destroy(cfg); ast_config_destroy(cfg);
return -1; return -1;
} }
memset(azone,0,sizeof(struct tone_zone));
ast_copy_string(azone->country, country, sizeof(azone->country)); ast_copy_string(azone->country, country, sizeof(azone->country));
ast_copy_string(azone->alias, cxt, sizeof(azone->alias)); ast_copy_string(azone->alias, cxt, sizeof(azone->alias));
if (ast_register_indication_country(azone)) { if (ast_register_indication_country(azone)) {
@ -326,9 +316,7 @@ static int ind_load_module(void)
} }
} }
/* not there, add it to the back */ /* not there, add it to the back */
ts = malloc(sizeof(struct tone_zone_sound)); if (!(ts = ast_malloc(sizeof(*ts)))) {
if (!ts) {
ast_log(LOG_WARNING, "Out of memory\n");
ast_config_destroy(cfg); ast_config_destroy(cfg);
return -1; return -1;
} }

@ -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 = ast_calloc(1, sizeof(*monitor)))) {
if (!monitor) {
UNLOCK_IF_NEEDED(&chan->lock, need_lock); UNLOCK_IF_NEEDED(&chan->lock, need_lock);
return -1; return -1;
} }
memset(monitor, 0, sizeof(struct ast_channel_monitor));
/* Determine file names */ /* Determine file names */
if (!ast_strlen_zero(fname_base)) { if (!ast_strlen_zero(fname_base)) {
@ -416,8 +414,8 @@ static int start_monitor_exec(struct ast_channel *chan, void *data)
if (urlprefix) { if (urlprefix) {
snprintf(tmp,sizeof(tmp) - 1,"%s/%s.%s",urlprefix,fname_base, snprintf(tmp,sizeof(tmp) - 1,"%s/%s.%s",urlprefix,fname_base,
((strcmp(format,"gsm")) ? "wav" : "gsm")); ((strcmp(format,"gsm")) ? "wav" : "gsm"));
if (!chan->cdr) if (!chan->cdr && !(chan->cdr = ast_cdr_alloc()))
chan->cdr = ast_cdr_alloc(); return -1;
ast_cdr_setuserfield(chan, tmp); ast_cdr_setuserfield(chan, tmp);
} }
if (waitforbridge) { if (waitforbridge) {
@ -492,16 +490,14 @@ static int start_monitor_action(struct mansession *s, struct message *m)
if (ast_strlen_zero(fname)) { if (ast_strlen_zero(fname)) {
/* No filename base specified, default to channel name as per CLI */ /* No filename base specified, default to channel name as per CLI */
fname = malloc (FILENAME_MAX); if (!(fname = ast_strdup(c->name))) {
if (!fname) {
astman_send_error(s, m, "Could not start monitoring channel"); astman_send_error(s, m, "Could not start monitoring channel");
ast_mutex_unlock(&c->lock); ast_mutex_unlock(&c->lock);
return 0; 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 / */ /* 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)) { if (ast_monitor_start(c, format, fname, 1)) {

@ -279,18 +279,17 @@ static void *moh_files_alloc(struct ast_channel *chan, void *params)
{ {
struct moh_files_state *state; struct moh_files_state *state;
struct mohclass *class = params; 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; chan->music_state = state;
allocated = 1; state->class = class;
} else } else
state = chan->music_state; state = chan->music_state;
if (state) { if (state) {
if (allocated || state->class != class) { if (state->class != class) {
/* initialize */ /* initialize */
memset(state, 0, sizeof(struct moh_files_state)); memset(state, 0, sizeof(*state));
state->class = class; state->class = class;
} }
@ -612,10 +611,8 @@ static struct mohdata *mohalloc(struct mohclass *cl)
{ {
struct mohdata *moh; struct mohdata *moh;
long flags; long flags;
moh = malloc(sizeof(struct mohdata)); if (!(moh = ast_calloc(1, sizeof(*moh))))
if (!moh)
return NULL; return NULL;
memset(moh, 0, sizeof(struct mohdata));
if (pipe(moh->pipe)) { if (pipe(moh->pipe)) {
ast_log(LOG_WARNING, "Failed to create pipe: %s\n", strerror(errno)); ast_log(LOG_WARNING, "Failed to create pipe: %s\n", strerror(errno));
free(moh); free(moh);
@ -669,8 +666,7 @@ static void *moh_alloc(struct ast_channel *chan, void *params)
struct mohdata *res; struct mohdata *res;
struct mohclass *class = params; struct mohclass *class = params;
res = mohalloc(class); if ((res = mohalloc(class))) {
if (res) {
res->origwfmt = chan->writeformat; res->origwfmt = chan->writeformat;
if (ast_set_write_format(chan, class->format)) { 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)); 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; 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; return class;
} }
@ -938,9 +928,7 @@ static int load_moh_classes(int reload)
cat = ast_category_browse(cfg, NULL); cat = ast_category_browse(cfg, NULL);
for (; cat; cat = ast_category_browse(cfg, cat)) { for (; cat; cat = ast_category_browse(cfg, cat)) {
if (strcasecmp(cat, "classes") && strcasecmp(cat, "moh_files")) { if (strcasecmp(cat, "classes") && strcasecmp(cat, "moh_files")) {
class = moh_class_malloc(); if (!(class = moh_class_malloc())) {
if (!class) {
ast_log(LOG_WARNING, "Out of memory!\n");
break; break;
} }
ast_copy_string(class->name, cat, sizeof(class->name)); ast_copy_string(class->name, cat, sizeof(class->name));
@ -1006,9 +994,7 @@ static int load_moh_classes(int reload)
if (args) if (args)
*args++ = '\0'; *args++ = '\0';
if (!(get_mohbyname(var->name))) { if (!(get_mohbyname(var->name))) {
class = moh_class_malloc(); if (!(class = moh_class_malloc())) {
if (!class) {
ast_log(LOG_WARNING, "Out of memory!\n");
return numclasses; return numclasses;
} }
@ -1034,9 +1020,7 @@ static int load_moh_classes(int reload)
args = strchr(var->value, ','); args = strchr(var->value, ',');
if (args) if (args)
*args++ = '\0'; *args++ = '\0';
class = moh_class_malloc(); if (!(class = moh_class_malloc())) {
if (!class) {
ast_log(LOG_WARNING, "Out of memory!\n");
return numclasses; return numclasses;
} }

@ -124,12 +124,9 @@ static int osp_build(struct ast_config *cfg, char *cat)
ast_mutex_unlock(&osplock); ast_mutex_unlock(&osplock);
if (!osp) { if (!osp) {
mallocd = 1; mallocd = 1;
osp = malloc(sizeof(struct osp_provider)); if (!(osp = ast_calloc(1, sizeof(*osp)))) {
if (!osp) {
ast_log(LOG_WARNING, "Out of memory!\n");
return -1; return -1;
} }
memset(osp, 0, sizeof(struct osp_provider));
osp->handle = -1; osp->handle = -1;
} }
ast_copy_string(osp->name, cat, sizeof(osp->name)); ast_copy_string(osp->name, cat, sizeof(osp->name));

Loading…
Cancel
Save