From 305867a9e08134b08790b919f7c90ca0bdee2cfe Mon Sep 17 00:00:00 2001 From: russell Date: Wed, 11 Jan 2006 01:20:29 +0000 Subject: convert some channels to use the memory allocation wrappers. (This is being added to the janitor projects list.) git-svn-id: http://svn.digium.com/svn/asterisk/trunk@7954 f38db490-d61c-443f-a65b-d21fe96a405b --- channels/chan_agent.c | 45 +++++------- channels/chan_alsa.c | 4 +- channels/chan_iax2.c | 187 ++++++++++++++++++++++---------------------------- channels/chan_local.c | 92 ++++++++++--------------- channels/chan_oss.c | 19 +++-- channels/chan_sip.c | 144 ++++++++++++++------------------------ channels/chan_zap.c | 93 +++++++++++-------------- 7 files changed, 238 insertions(+), 346 deletions(-) (limited to 'channels') diff --git a/channels/chan_agent.c b/channels/chan_agent.c index d2fe2d98c..c22c650ad 100644 --- a/channels/chan_agent.c +++ b/channels/chan_agent.c @@ -350,25 +350,20 @@ static struct agent_pvt *add_agent(char *agent, int pending) } if (!p) { // Build the agent. - p = malloc(sizeof(struct agent_pvt)); - if (p) { - memset(p, 0, sizeof(struct agent_pvt)); - ast_copy_string(p->agent, agt, sizeof(p->agent)); - ast_mutex_init(&p->lock); - ast_mutex_init(&p->app_lock); - p->owning_app = (pthread_t) -1; - p->app_sleep_cond = 1; - p->group = group; - p->pending = pending; - p->next = NULL; - if (prev) - prev->next = p; - else - agents = p; - - } else { + if (!(p = ast_calloc(1, sizeof(*p)))) return NULL; - } + ast_copy_string(p->agent, agt, sizeof(p->agent)); + ast_mutex_init(&p->lock); + ast_mutex_init(&p->app_lock); + p->owning_app = (pthread_t) -1; + p->app_sleep_cond = 1; + p->group = group; + p->pending = pending; + p->next = NULL; + if (prev) + prev->next = p; + else + agents = p; } ast_copy_string(p->password, password ? password : "", sizeof(p->password)); @@ -666,16 +661,10 @@ static int agent_call(struct ast_channel *ast, char *dest, int timeout) ast_verbose(VERBOSE_PREFIX_3 "outgoing agentcall, to agent '%s', on '%s'\n", p->agent, p->chan->name); if (p->chan->cid.cid_num) free(p->chan->cid.cid_num); - if (ast->cid.cid_num) - p->chan->cid.cid_num = strdup(ast->cid.cid_num); - else - p->chan->cid.cid_num = NULL; + p->chan->cid.cid_num = ast_strdup(ast->cid.cid_num); if (p->chan->cid.cid_name) free(p->chan->cid.cid_name); - if (ast->cid.cid_name) - p->chan->cid.cid_name = strdup(ast->cid.cid_name); - else - p->chan->cid.cid_name = NULL; + p->chan->cid.cid_name = ast_strdup(ast->cid.cid_name); ast_channel_inherit_variables(ast, p->chan); res = ast_call(p->chan, p->loginchan, 0); CLEANUP(ast,p); @@ -1568,12 +1557,12 @@ static char *complete_agent_logoff_cmd(char *line, char *word, int pos, int stat snprintf(name, sizeof(name), "Agent/%s", p->agent); if (!strncasecmp(word, name, strlen(word))) { if (++which > state) { - return strdup(name); + return ast_strdup(name); } } } } else if (pos == 3 && state == 0) { - return strdup("soft"); + return ast_strdup("soft"); } return NULL; } diff --git a/channels/chan_alsa.c b/channels/chan_alsa.c index f427ac730..45cd1c671 100644 --- a/channels/chan_alsa.c +++ b/channels/chan_alsa.c @@ -865,10 +865,10 @@ static char *autoanswer_complete(char *line, char *word, int pos, int state) switch(state) { case 0: if (!ast_strlen_zero(word) && !strncasecmp(word, "on", MIN(strlen(word), 2))) - return strdup("on"); + return ast_strdup("on"); case 1: if (!ast_strlen_zero(word) && !strncasecmp(word, "off", MIN(strlen(word), 3))) - return strdup("off"); + return ast_strdup("off"); default: return NULL; } diff --git a/channels/chan_iax2.c b/channels/chan_iax2.c index 01ee3fa47..8e5bf2a79 100644 --- a/channels/chan_iax2.c +++ b/channels/chan_iax2.c @@ -892,35 +892,35 @@ static int iax2_getpeername(struct sockaddr_in sin, char *host, int len, int loc static struct chan_iax2_pvt *new_iax(struct sockaddr_in *sin, int lockpeer, const char *host) { struct chan_iax2_pvt *tmp; - tmp = malloc(sizeof(struct chan_iax2_pvt)); - if (tmp) { - memset(tmp, 0, sizeof(struct chan_iax2_pvt)); - tmp->prefs = prefs; - tmp->callno = 0; - tmp->peercallno = 0; - tmp->transfercallno = 0; - tmp->bridgecallno = 0; - tmp->pingid = -1; - tmp->lagid = -1; - tmp->autoid = -1; - tmp->authid = -1; - tmp->initid = -1; - /* ast_copy_string(tmp->context, context, sizeof(tmp->context)); */ - ast_copy_string(tmp->exten, "s", sizeof(tmp->exten)); - ast_copy_string(tmp->host, host, sizeof(tmp->host)); + + if (!(tmp = ast_calloc(1, sizeof(*tmp)))) + return NULL; + + tmp->prefs = prefs; + tmp->callno = 0; + tmp->peercallno = 0; + tmp->transfercallno = 0; + tmp->bridgecallno = 0; + tmp->pingid = -1; + tmp->lagid = -1; + tmp->autoid = -1; + tmp->authid = -1; + tmp->initid = -1; + /* ast_copy_string(tmp->context, context, sizeof(tmp->context)); */ + ast_copy_string(tmp->exten, "s", sizeof(tmp->exten)); + ast_copy_string(tmp->host, host, sizeof(tmp->host)); #ifdef NEWJB - { - jb_conf jbconf; + { + jb_conf jbconf; - tmp->jb = jb_new(); - tmp->jbid = -1; - jbconf.max_jitterbuf = maxjitterbuffer; - jbconf.resync_threshold = resyncthreshold; - jbconf.max_contig_interp = maxjitterinterps; - jb_setconf(tmp->jb,&jbconf); - } -#endif + tmp->jb = jb_new(); + tmp->jbid = -1; + jbconf.max_jitterbuf = maxjitterbuffer; + jbconf.resync_threshold = resyncthreshold; + jbconf.max_contig_interp = maxjitterinterps; + jb_setconf(tmp->jb,&jbconf); } +#endif return tmp; } @@ -1275,9 +1275,7 @@ static int try_firmware(char *s) } if (!cur) { /* Allocate a new one and link it */ - cur = malloc(sizeof(struct iax_firmware)); - if (cur) { - memset(cur, 0, sizeof(struct iax_firmware)); + if ((cur = ast_calloc(1, sizeof(*cur)))) { cur->fd = -1; cur->next = waresl.wares; waresl.wares = cur; @@ -1985,7 +1983,7 @@ static char *complete_iax2_show_peer(char *line, char *word, int pos, int state) for (p = peerl.peers ; p ; p = p->next) { if (!strncasecmp(p->name, word, wordlen)) { if (++which > state) { - res = strdup(p->name); + res = ast_strdup(p->name); break; } } @@ -3085,20 +3083,17 @@ static int iax2_setoption(struct ast_channel *c, int option, void *data, int dat errno = ENOSYS; return -1; default: - h = malloc(datalen + sizeof(*h)); - if (h) { - h->flag = AST_OPTION_FLAG_REQUEST; - h->option = htons(option); - memcpy(h->data, data, datalen); - res = send_command_locked(PTR_TO_CALLNO(c->tech_pvt), AST_FRAME_CONTROL, - AST_CONTROL_OPTION, 0, (unsigned char *) h, - datalen + sizeof(*h), -1); - free(h); - return res; - } else { - ast_log(LOG_WARNING, "Out of memory\n"); + if (!(h = ast_malloc(datalen + sizeof(*h)))) return -1; - } + + h->flag = AST_OPTION_FLAG_REQUEST; + h->option = htons(option); + memcpy(h->data, data, datalen); + res = send_command_locked(PTR_TO_CALLNO(c->tech_pvt), AST_FRAME_CONTROL, + AST_CONTROL_OPTION, 0, (unsigned char *) h, + datalen + sizeof(*h), -1); + free(h); + return res; } } @@ -3398,15 +3393,15 @@ static struct ast_channel *ast_iax2_new(int callno, int state, int capability) tmp->tech_pvt = CALLNO_TO_PTR(i->callno); if (!ast_strlen_zero(i->cid_num)) - tmp->cid.cid_num = strdup(i->cid_num); + tmp->cid.cid_num = ast_strdup(i->cid_num); if (!ast_strlen_zero(i->cid_name)) - tmp->cid.cid_name = strdup(i->cid_name); + tmp->cid.cid_name = ast_strdup(i->cid_name); if (!ast_strlen_zero(i->ani)) - tmp->cid.cid_ani = strdup(i->ani); + tmp->cid.cid_ani = ast_strdup(i->ani); if (!ast_strlen_zero(i->language)) ast_copy_string(tmp->language, i->language, sizeof(tmp->language)); if (!ast_strlen_zero(i->dnid)) - tmp->cid.cid_dnid = strdup(i->dnid); + tmp->cid.cid_dnid = ast_strdup(i->dnid); tmp->cid.cid_pres = i->calling_pres; tmp->cid.cid_ton = i->calling_ton; tmp->cid.cid_tns = i->calling_tns; @@ -3685,9 +3680,7 @@ static struct iax2_trunk_peer *find_tpeer(struct sockaddr_in *sin, int fd) tpeer = tpeer->next; } if (!tpeer) { - tpeer = malloc(sizeof(struct iax2_trunk_peer)); - if (tpeer) { - memset(tpeer, 0, sizeof(struct iax2_trunk_peer)); + if ((tpeer = ast_calloc(1, sizeof(*tpeer)))) { ast_mutex_init(&tpeer->lock); tpeer->lastsent = 9999; memcpy(&tpeer->addr, sin, sizeof(tpeer->addr)); @@ -3721,16 +3714,14 @@ static int iax2_trunk_queue(struct chan_iax2_pvt *pvt, struct iax_frame *fr) if (tpeer->trunkdatalen + f->datalen + 4 >= tpeer->trunkdataalloc) { /* Need to reallocate space */ if (tpeer->trunkdataalloc < MAX_TRUNKDATA) { - tmp = realloc(tpeer->trunkdata, tpeer->trunkdataalloc + DEFAULT_TRUNKDATA + IAX2_TRUNK_PREFACE); - if (tmp) { - tpeer->trunkdataalloc += DEFAULT_TRUNKDATA; - tpeer->trunkdata = tmp; - ast_log(LOG_DEBUG, "Expanded trunk '%s:%d' to %d bytes\n", ast_inet_ntoa(iabuf, sizeof(iabuf), tpeer->addr.sin_addr), ntohs(tpeer->addr.sin_port), tpeer->trunkdataalloc); - } else { - ast_log(LOG_WARNING, "Insufficient memory to expand trunk data to %s:%d\n", ast_inet_ntoa(iabuf, sizeof(iabuf), tpeer->addr.sin_addr), ntohs(tpeer->addr.sin_port)); + if (!(tmp = ast_realloc(tpeer->trunkdata, tpeer->trunkdataalloc + DEFAULT_TRUNKDATA + IAX2_TRUNK_PREFACE))) { ast_mutex_unlock(&tpeer->lock); return -1; } + + tpeer->trunkdataalloc += DEFAULT_TRUNKDATA; + tpeer->trunkdata = tmp; + ast_log(LOG_DEBUG, "Expanded trunk '%s:%d' to %d bytes\n", ast_inet_ntoa(iabuf, sizeof(iabuf), tpeer->addr.sin_addr), ntohs(tpeer->addr.sin_port), tpeer->trunkdataalloc); } else { ast_log(LOG_WARNING, "Maximum trunk data space exceeded to %s:%d\n", ast_inet_ntoa(iabuf, sizeof(iabuf), tpeer->addr.sin_addr), ntohs(tpeer->addr.sin_port)); ast_mutex_unlock(&tpeer->lock); @@ -5511,24 +5502,20 @@ static int iax2_register(char *value, int lineno) ast_log(LOG_WARNING, "Host '%s' not found at line %d\n", hostname, lineno); return -1; } - reg = malloc(sizeof(struct iax2_registry)); - if (reg) { - memset(reg, 0, sizeof(struct iax2_registry)); - ast_copy_string(reg->username, username, sizeof(reg->username)); - if (secret) - ast_copy_string(reg->secret, secret, sizeof(reg->secret)); - reg->expire = -1; - reg->refresh = IAX_DEFAULT_REG_EXPIRE; - reg->addr.sin_family = AF_INET; - memcpy(®->addr.sin_addr, hp->h_addr, sizeof(®->addr.sin_addr)); - reg->addr.sin_port = porta ? htons(atoi(porta)) : htons(IAX_DEFAULT_PORTNO); - reg->next = registrations; - reg->callno = 0; - registrations = reg; - } else { - ast_log(LOG_ERROR, "Out of memory\n"); + if (!(reg = ast_calloc(1, sizeof(*reg)))) return -1; - } + ast_copy_string(reg->username, username, sizeof(reg->username)); + if (secret) + ast_copy_string(reg->secret, secret, sizeof(reg->secret)); + reg->expire = -1; + reg->refresh = IAX_DEFAULT_REG_EXPIRE; + reg->addr.sin_family = AF_INET; + memcpy(®->addr.sin_addr, hp->h_addr, sizeof(®->addr.sin_addr)); + reg->addr.sin_port = porta ? htons(atoi(porta)) : htons(IAX_DEFAULT_PORTNO); + reg->next = registrations; + reg->callno = 0; + registrations = reg; + return 0; } @@ -5542,7 +5529,7 @@ static void register_peer_exten(struct iax2_peer *peer, int onoff) while((ext = strsep(&stringp, "&"))) { if (onoff) { if (!ast_exists_extension(NULL, regcontext, ext, 1, NULL)) - ast_add_extension(regcontext, 1, ext, 1, NULL, NULL, "Noop", strdup(peer->name), free, channeltype); + ast_add_extension(regcontext, 1, ext, 1, NULL, NULL, "Noop", ast_strdup(peer->name), free, channeltype); } else ast_context_remove_extension(regcontext, ext, 1, NULL); } @@ -6097,19 +6084,18 @@ static void spawn_dp_lookup(int callno, char *context, char *callednum, char *ca { pthread_t newthread; struct dpreq_data *dpr; - dpr = malloc(sizeof(struct dpreq_data)); - if (dpr) { - memset(dpr, 0, sizeof(struct dpreq_data)); - dpr->callno = callno; - ast_copy_string(dpr->context, context, sizeof(dpr->context)); - ast_copy_string(dpr->callednum, callednum, sizeof(dpr->callednum)); - if (callerid) - dpr->callerid = strdup(callerid); - if (ast_pthread_create(&newthread, NULL, dp_lookup_thread, dpr)) { - ast_log(LOG_WARNING, "Unable to start lookup thread!\n"); - } - } else - ast_log(LOG_WARNING, "Out of memory!\n"); + + if (!(dpr = ast_calloc(1, sizeof(*dpr)))) + return; + + dpr->callno = callno; + ast_copy_string(dpr->context, context, sizeof(dpr->context)); + ast_copy_string(dpr->callednum, callednum, sizeof(dpr->callednum)); + if (callerid) + dpr->callerid = ast_strdup(callerid); + if (ast_pthread_create(&newthread, NULL, dp_lookup_thread, dpr)) { + ast_log(LOG_WARNING, "Unable to start lookup thread!\n"); + } } struct iax_dual { @@ -6178,9 +6164,7 @@ static int iax_park(struct ast_channel *chan1, struct ast_channel *chan2) ast_hangup(chan2m); return -1; } - d = malloc(sizeof(struct iax_dual)); - if (d) { - memset(d, 0, sizeof(*d)); + if ((d = ast_calloc(1, sizeof(*d)))) { d->chan1 = chan1m; d->chan2 = chan2m; if (!ast_pthread_create(&th, NULL, iax_park_thread, d)) @@ -7988,11 +7972,11 @@ static int start_network_thread(void) static struct iax2_context *build_context(char *context) { - struct iax2_context *con = malloc(sizeof(struct iax2_context)); - if (con) { + struct iax2_context *con; + + if ((con = ast_calloc(1, sizeof(*con)))) ast_copy_string(con->context, context, sizeof(con->context)); - con->next = NULL; - } + return con; } @@ -8129,9 +8113,7 @@ static struct iax2_peer *build_peer(const char *name, struct ast_variable *v, in ast_mutex_unlock(&peerl.lock); } else { ast_mutex_unlock(&peerl.lock); - peer = malloc(sizeof(struct iax2_peer)); - if (peer) { - memset(peer, 0, sizeof(struct iax2_peer)); + if ((peer = ast_calloc(1, sizeof(*peer)))) { peer->expire = -1; peer->pokeexpire = -1; peer->sockfd = defaultsockfd; @@ -8327,9 +8309,8 @@ static struct iax2_user *build_user(const char *name, struct ast_variable *v, in ast_mutex_unlock(&userl.lock); } else { ast_mutex_unlock(&userl.lock); - user = malloc(sizeof(struct iax2_user)); - if (user) - memset(user, 0, sizeof(struct iax2_user)); + /* This is going to memset'd to 0 in the next block */ + user = ast_malloc(sizeof(*user)); } if (user) { @@ -8965,12 +8946,10 @@ static struct iax2_dpcache *find_cache(struct ast_channel *chan, const char *dat ast_log(LOG_WARNING, "Unable to generate call for '%s'\n", data); return NULL; } - dp = malloc(sizeof(struct iax2_dpcache)); - if (!dp) { + if (!(dp = ast_calloc(1, sizeof(*dp)))) { ast_mutex_unlock(&iaxsl[callno]); return NULL; } - memset(dp, 0, sizeof(struct iax2_dpcache)); ast_copy_string(dp->peercontext, data, sizeof(dp->peercontext)); ast_copy_string(dp->exten, exten, sizeof(dp->exten)); gettimeofday(&dp->expiry, NULL); diff --git a/channels/chan_local.c b/channels/chan_local.c index e5d1ea5cb..44103de50 100644 --- a/channels/chan_local.c +++ b/channels/chan_local.c @@ -326,25 +326,11 @@ static int local_call(struct ast_channel *ast, char *dest, int timeout) size_t len, namelen; ast_mutex_lock(&p->lock); - if (p->owner->cid.cid_num) - p->chan->cid.cid_num = strdup(p->owner->cid.cid_num); - else - p->chan->cid.cid_num = NULL; - - if (p->owner->cid.cid_name) - p->chan->cid.cid_name = strdup(p->owner->cid.cid_name); - else - p->chan->cid.cid_name = NULL; - - if (p->owner->cid.cid_rdnis) - p->chan->cid.cid_rdnis = strdup(p->owner->cid.cid_rdnis); - else - p->chan->cid.cid_rdnis = NULL; - if (p->owner->cid.cid_ani) - p->chan->cid.cid_ani = strdup(p->owner->cid.cid_ani); - else - p->chan->cid.cid_ani = NULL; + p->chan->cid.cid_num = ast_strdup(p->owner->cid.cid_num); + p->chan->cid.cid_name = ast_strdup(p->owner->cid.cid_name); + p->chan->cid.cid_rdnis = ast_strdup(p->owner->cid.cid_rdnis); + p->chan->cid.cid_ani = ast_strdup(p->owner->cid.cid_ani); strncpy(p->chan->language, p->owner->language, sizeof(p->chan->language) - 1); strncpy(p->chan->accountcode, p->owner->accountcode, sizeof(p->chan->accountcode) - 1); @@ -355,13 +341,10 @@ static int local_call(struct ast_channel *ast, char *dest, int timeout) AST_LIST_TRAVERSE(&p->owner->varshead, varptr, entries) { namelen = strlen(varptr->name); len = sizeof(struct ast_var_t) + namelen + strlen(varptr->value) + 2; - new = malloc(len); - if (new) { + if ((new = ast_calloc(1, len))) { memcpy(new, varptr, len); new->value = &(new->name[0]) + namelen + 1; AST_LIST_INSERT_TAIL(&p->chan->varshead, new, entries); - } else { - ast_log(LOG_ERROR, "Out of memory!\n"); } } @@ -472,40 +455,39 @@ static struct local_pvt *local_alloc(char *data, int format) char *c; char *opts; - tmp = malloc(sizeof(struct local_pvt)); - if (tmp) { - memset(tmp, 0, sizeof(struct local_pvt)); - ast_mutex_init(&tmp->lock); - strncpy(tmp->exten, data, sizeof(tmp->exten) - 1); - opts = strchr(tmp->exten, '/'); - if (opts) { - *opts='\0'; - opts++; - if (strchr(opts, 'n')) - tmp->nooptimization = 1; - } - c = strchr(tmp->exten, '@'); - if (c) { - *c = '\0'; - c++; - strncpy(tmp->context, c, sizeof(tmp->context) - 1); - } else - strncpy(tmp->context, "default", sizeof(tmp->context) - 1); - tmp->reqformat = format; - if (!ast_exists_extension(NULL, tmp->context, tmp->exten, 1, NULL)) { - ast_log(LOG_NOTICE, "No such extension/context %s@%s creating local channel\n", tmp->exten, tmp->context); - ast_mutex_destroy(&tmp->lock); - free(tmp); - tmp = NULL; - } else { - /* Add to list */ - ast_mutex_lock(&locallock); - tmp->next = locals; - locals = tmp; - ast_mutex_unlock(&locallock); - } - + if (!(tmp = ast_calloc(1, sizeof(*tmp)))) + return NULL; + + ast_mutex_init(&tmp->lock); + strncpy(tmp->exten, data, sizeof(tmp->exten) - 1); + opts = strchr(tmp->exten, '/'); + if (opts) { + *opts='\0'; + opts++; + if (strchr(opts, 'n')) + tmp->nooptimization = 1; } + c = strchr(tmp->exten, '@'); + if (c) { + *c = '\0'; + c++; + strncpy(tmp->context, c, sizeof(tmp->context) - 1); + } else + strncpy(tmp->context, "default", sizeof(tmp->context) - 1); + tmp->reqformat = format; + if (!ast_exists_extension(NULL, tmp->context, tmp->exten, 1, NULL)) { + ast_log(LOG_NOTICE, "No such extension/context %s@%s creating local channel\n", tmp->exten, tmp->context); + ast_mutex_destroy(&tmp->lock); + free(tmp); + tmp = NULL; + } else { + /* Add to list */ + ast_mutex_lock(&locallock); + tmp->next = locals; + locals = tmp; + ast_mutex_unlock(&locallock); + } + return tmp; } diff --git a/channels/chan_oss.c b/channels/chan_oss.c index 9d0481d2a..68d79c7b5 100644 --- a/channels/chan_oss.c +++ b/channels/chan_oss.c @@ -380,7 +380,7 @@ static char *ast_ext_ctx(const char *src, char **ext, char **ctx) return NULL; /* error */ *ext = *ctx = NULL; if (src && *src != '\0') - *ext = strdup(src); + *ext = ast_strdup(src); if (*ext == NULL) return NULL; if (!o->overridecontext) { @@ -899,9 +899,9 @@ static struct ast_channel *oss_new(struct chan_oss_pvt *o, if (!ast_strlen_zero(o->language)) ast_copy_string(c->language, o->language, sizeof(c->language)); if (!ast_strlen_zero(o->cid_num)) - c->cid.cid_num = strdup(o->cid_num); + c->cid.cid_num = ast_strdup(o->cid_num); if (!ast_strlen_zero(o->cid_name)) - c->cid.cid_name = strdup(o->cid_name); + c->cid.cid_name = ast_strdup(o->cid_name); o->owner = c; ast_setstate(c, state); @@ -982,10 +982,10 @@ static char *autoanswer_complete(char *line, char *word, int pos, int state) switch(state) { case 0: if (l && !strncasecmp(word, "on", MIN(l, 2))) - return strdup("on"); + return ast_strdup("on"); case 1: if (l && !strncasecmp(word, "off", MIN(l, 3))) - return strdup("off"); + return ast_strdup("off"); default: return NULL; } @@ -1280,7 +1280,7 @@ static void store_mixer(struct chan_oss_pvt *o, char *s) } if (o->mixer_cmd) free(o->mixer_cmd); - o->mixer_cmd = strdup(s); + o->mixer_cmd = ast_strdup(s); ast_log(LOG_WARNING, "setting mixer %s\n", s); } @@ -1304,17 +1304,16 @@ static struct chan_oss_pvt * store_config(struct ast_config *cfg, char *ctg) o = &oss_default; ctg = "general"; } else { - o = (struct chan_oss_pvt *)malloc(sizeof *o); - if (o == NULL) /* fail */ + if (!(o = ast_calloc(1, sizeof(*o)))) return NULL; *o = oss_default; /* "general" is also the default thing */ if (strcmp(ctg, "general") == 0) { - o->name = strdup("dsp"); + o->name = ast_strdup("dsp"); oss_active = o->name; goto openit; } - o->name = strdup(ctg); + o->name = ast_strdup(ctg); } o->lastopen = ast_tvnow(); /* don't leave it 0 or tvdiff may wrap */ diff --git a/channels/chan_sip.c b/channels/chan_sip.c index 632b79107..3c1669808 100644 --- a/channels/chan_sip.c +++ b/channels/chan_sip.c @@ -1149,15 +1149,9 @@ static void append_history_va(struct sip_pvt *p, const char *fmt, va_list ap) vsnprintf(buf, sizeof(buf), fmt, ap); strsep(&c, "\r\n"); /* Trim up everything after \r or \n */ l = strlen(buf) + 1; - hist = calloc(1, sizeof(*hist) + l); - if (!hist) { - ast_log(LOG_WARNING, "Can't allocate memory for history"); + if (!(hist = ast_calloc(1, sizeof(*hist) + l))) return; - } - if (p->history == NULL) - p->history = calloc(1, sizeof(struct sip_history_head)); - if (p->history == NULL) { - ast_log(LOG_WARNING, "Can't allocate memory for history head"); + if (!p->history && !(p->history = ast_calloc(1, sizeof(*p->history)))) { free(hist); return; } @@ -1283,10 +1277,8 @@ static int __sip_reliable_xmit(struct sip_pvt *p, int seqno, int resp, char *dat struct sip_pkt *pkt; int siptimer_a = DEFAULT_RETRANS; - pkt = malloc(sizeof(struct sip_pkt) + len + 1); - if (!pkt) + if (!(pkt = ast_calloc(1, sizeof(*pkt) + len + 1))) return -1; - memset(pkt, 0, sizeof(struct sip_pkt)); memcpy(pkt->data, data, len); pkt->method = sipmethod; pkt->packetlen = len; @@ -1616,7 +1608,7 @@ static void register_peer_exten(struct sip_peer *peer, int onoff) stringp = multi; while((ext = strsep(&stringp, "&"))) { if (onoff) - ast_add_extension(regcontext, 1, ext, 1, NULL, NULL, "Noop", strdup(peer->name), free, channeltype); + ast_add_extension(regcontext, 1, ext, 1, NULL, NULL, "Noop", ast_strdup(peer->name), free, channeltype); else ast_context_remove_extension(regcontext, ext, 1, NULL); } @@ -2847,13 +2839,13 @@ static struct ast_channel *sip_new(struct sip_pvt *i, int state, const char *tit ast_copy_string(tmp->context, i->context, sizeof(tmp->context)); ast_copy_string(tmp->exten, i->exten, sizeof(tmp->exten)); if (!ast_strlen_zero(i->cid_num)) - tmp->cid.cid_num = strdup(i->cid_num); + tmp->cid.cid_num = ast_strdup(i->cid_num); if (!ast_strlen_zero(i->cid_name)) - tmp->cid.cid_name = strdup(i->cid_name); + tmp->cid.cid_name = ast_strdup(i->cid_name); if (!ast_strlen_zero(i->rdnis)) - tmp->cid.cid_rdnis = strdup(i->rdnis); + tmp->cid.cid_rdnis = ast_strdup(i->rdnis); if (!ast_strlen_zero(i->exten) && strcmp(i->exten, "s")) - tmp->cid.cid_dnid = strdup(i->exten); + tmp->cid.cid_dnid = ast_strdup(i->exten); tmp->priority = 1; if (!ast_strlen_zero(i->uri)) { pbx_builtin_setvar_helper(tmp, "SIPURI", i->uri); @@ -3097,7 +3089,7 @@ static struct sip_pvt *sip_alloc(ast_string_field callid, struct sockaddr_in *si { struct sip_pvt *p; - if (!(p = calloc(1, sizeof(*p)))) + if (!(p = ast_calloc(1, sizeof(*p)))) return NULL; if (ast_string_field_init(p)) { @@ -3310,10 +3302,8 @@ static int sip_register(char *value, int lineno) ast_log(LOG_WARNING, "%s is not a valid port number at line %d\n", porta, lineno); return -1; } - if (!(reg = calloc(1, sizeof(*reg)))) { - ast_log(LOG_ERROR, "Out of memory. Can't allocate SIP registry entry\n"); + if (!(reg = ast_calloc(1, sizeof(*reg)))) return -1; - } if (ast_string_field_init(reg)) { ast_log(LOG_ERROR, "Out of memory. Can't allocate SIP registry entry\n"); @@ -6066,12 +6056,13 @@ static void build_route(struct sip_pvt *p, struct sip_request *req, int backward /* Each route entry */ /* Find < */ rr = strchr(rr, '<'); - if (!rr) break; /* No more hops */ + if (!rr) + break; /* No more hops */ ++rr; len = strcspn(rr, ">") + 1; /* Make a struct route */ - thishop = malloc(sizeof(*thishop) + len); - if (thishop) { + if ((thishop = ast_malloc(sizeof(*thishop) + len))) { + /* ast_calloc is not needed because all fields are initialized in this block */ ast_copy_string(thishop->hop, rr, len); ast_log(LOG_DEBUG, "build_route: Record-Route hop: <%s>\n", thishop->hop); /* Link in */ @@ -6113,8 +6104,8 @@ static void build_route(struct sip_pvt *p, struct sip_request *req, int backward c = contact; len = strlen(contact) + 1; } - thishop = malloc(sizeof(*thishop) + len); - if (thishop) { + if ((thishop = ast_malloc(sizeof(*thishop) + len))) { + /* ast_calloc is not needed because all fields are initialized in this block */ ast_copy_string(thishop->hop, c, len); thishop->next = NULL; /* Goes at the end */ @@ -8421,7 +8412,7 @@ static char *complete_sipch(char *line, char *word, int pos, int state) for (cur = iflist; cur; cur = cur->next) { if (!strncasecmp(word, cur->callid, wordlen)) { if (++which > state) { - c = strdup(cur->callid); + c = ast_strdup(cur->callid); break; } } @@ -8443,7 +8434,7 @@ static char *complete_sip_peer(char *word, int state, int flags2) if (flags2 && !ast_test_flag((&iterator->flags_page2), flags2)) continue; if (++which > state) { - result = strdup(iterator->name); + result = ast_strdup(iterator->name); } } } while(0) ); @@ -8481,7 +8472,7 @@ static char *complete_sip_user(char *word, int state, int flags2) if (flags2 && !ast_test_flag(&(iterator->flags_page2), flags2)) continue; if (++which > state) { - result = strdup(iterator->name); + result = ast_strdup(iterator->name); } } } while(0) ); @@ -8515,7 +8506,7 @@ static char *complete_sipnotify(char *line, char *word, int pos, int state) while ( (cat = ast_category_browse(notify_types, cat)) ) { if (!strncasecmp(word, cat, wordlen)) { if (++which > state) { - c = strdup(cat); + c = ast_strdup(cat); break; } } @@ -8960,13 +8951,8 @@ static int do_proxy_auth(struct sip_pvt *p, struct sip_request *req, char *heade { char digest[1024]; - if (!p->options) { - p->options = calloc(1, sizeof(*p->options)); - if (!p->options) { - ast_log(LOG_ERROR, "Out of memory\n"); - return -2; - } - } + if (!p->options && !(p->options = ast_calloc(1, sizeof(*p->options)))) + return -2; p->authtries++; if (option_debug > 1) @@ -10181,9 +10167,7 @@ static int sip_park(struct ast_channel *chan1, struct ast_channel *chan2, struct return -1; } ast_mutex_unlock(&chan2m->lock); - d = malloc(sizeof(struct sip_dual)); - if (d) { - memset(d, 0, sizeof(*d)); + if ((d = ast_calloc(1, sizeof(*d)))) { /* Save original request for followup */ copy_request(&d->req, req); d->chan1 = chan1m; @@ -11259,11 +11243,9 @@ static int sip_send_mwi_to_peer(struct sip_peer *peer) return 0; } - p = sip_alloc(NULL, NULL, 0, SIP_NOTIFY); - if (!p) { - ast_log(LOG_WARNING, "Unable to build sip pvt data for MWI\n"); + if (!(p = sip_alloc(NULL, NULL, 0, SIP_NOTIFY))) return -1; - } + peer->lastmsgssent = ((newmsgs << 8) | (oldmsgs)); if (create_addr_from_peer(p, peer)) { /* Maybe they're not registered, etc. */ @@ -11487,11 +11469,9 @@ static int sip_poke_peer(struct sip_peer *peer) ast_log(LOG_NOTICE, "Still have a QUALIFY dialog active, deleting\n"); sip_destroy(peer->call); } - p = peer->call = sip_alloc(NULL, NULL, 0, SIP_OPTIONS); - if (!peer->call) { - ast_log(LOG_WARNING, "Unable to allocate dialog for poking peer '%s'\n", peer->name); + if (!(p = peer->call = sip_alloc(NULL, NULL, 0, SIP_OPTIONS))) return -1; - } + memcpy(&p->sa, &peer->addr, sizeof(p->sa)); memcpy(&p->recv, &peer->addr, sizeof(p->sa)); @@ -11612,17 +11592,11 @@ static struct ast_channel *sip_request_call(const char *type, int format, void * ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format %s while capability is %s\n", ast_getformatname(oldformat), ast_getformatname(global_capability)); return NULL; } - p = sip_alloc(NULL, NULL, 0, SIP_INVITE); - if (!p) { - ast_log(LOG_WARNING, "Unable to build sip pvt data for '%s'\n", (char *)data); + if (!(p = sip_alloc(NULL, NULL, 0, SIP_INVITE))) return NULL; - } - p->options = calloc(1, sizeof(*p->options)); - if (!p->options) { - ast_log(LOG_ERROR, "Out of memory\n"); + if (!(p->options = ast_calloc(1, sizeof(*p->options)))) return NULL; - } ast_copy_string(tmp, dest, sizeof(tmp)); host = strchr(tmp, '@'); @@ -11796,11 +11770,8 @@ static int add_sip_domain(const char *domain, const enum domain_mode mode, const return 1; } - d = calloc(1, sizeof(*d)); - if (!d) { - ast_log(LOG_ERROR, "Allocation of domain structure failed, Out of memory\n"); + if (!(d = ast_calloc(1, sizeof(*d)))) return 0; - } ast_copy_string(d->domain, domain, sizeof(d->domain)); @@ -11889,19 +11860,15 @@ static struct sip_auth *add_realm_authentication(struct sip_auth *authlist, char md5secret = strsep(&stringp,"#"); } } - auth = malloc(sizeof(struct sip_auth)); - if (auth) { - memset(auth, 0, sizeof(struct sip_auth)); - ast_copy_string(auth->realm, realm, sizeof(auth->realm)); - ast_copy_string(auth->username, username, sizeof(auth->username)); - if (secret) - ast_copy_string(auth->secret, secret, sizeof(auth->secret)); - if (md5secret) - ast_copy_string(auth->md5secret, md5secret, sizeof(auth->md5secret)); - } else { - ast_log(LOG_ERROR, "Allocation of auth structure failed, Out of memory\n"); + if (!(auth = ast_calloc(1, sizeof(*auth)))) return authlist; - } + + ast_copy_string(auth->realm, realm, sizeof(auth->realm)); + ast_copy_string(auth->username, username, sizeof(auth->username)); + if (secret) + ast_copy_string(auth->secret, secret, sizeof(auth->secret)); + if (md5secret) + ast_copy_string(auth->md5secret, md5secret, sizeof(auth->md5secret)); /* Add authentication to authl */ if (!authlist) { /* No existing list */ @@ -11960,11 +11927,9 @@ static struct sip_user *build_user(const char *name, struct ast_variable *v, int struct ast_flags mask = {(0)}; - user = (struct sip_user *)malloc(sizeof(struct sip_user)); - if (!user) { + if (!(user = ast_calloc(1, sizeof(*user)))) return NULL; - } - memset(user, 0, sizeof(struct sip_user)); + suserobjs++; ASTOBJ_INIT(user); ast_copy_string(user->name, name, sizeof(user->name)); @@ -12045,11 +12010,9 @@ static struct sip_peer *temp_peer(const char *name) { struct sip_peer *peer; - peer = malloc(sizeof(*peer)); - if (!peer) + if (!(peer = ast_calloc(1, sizeof(*peer)))) return NULL; - memset(peer, 0, sizeof(*peer)); apeerobjs++; ASTOBJ_INIT(peer); @@ -12102,23 +12065,18 @@ static struct sip_peer *build_peer(const char *name, struct ast_variable *v, int /* Already in the list, remove it and it will be added back (or FREE'd) */ found++; } else { - peer = malloc(sizeof(*peer)); - if (peer) { - memset(peer, 0, sizeof(*peer)); - if (realtime) - rpeerobjs++; - else - speerobjs++; - ASTOBJ_INIT(peer); - peer->expire = -1; - peer->pokeexpire = -1; - } else { - ast_log(LOG_WARNING, "Can't allocate SIP peer memory\n"); - } + if (!(peer = ast_calloc(1, sizeof(*peer)))) + return -1; + + if (realtime) + rpeerobjs++; + else + speerobjs++; + ASTOBJ_INIT(peer); + peer->expire = -1; + peer->pokeexpire = -1; } /* Note that our peer HAS had its reference count incrased */ - if (!peer) - return NULL; peer->lastmsgssent = -1; if (!found) { diff --git a/channels/chan_zap.c b/channels/chan_zap.c index 33edf6f6a..f416b19ae 100644 --- a/channels/chan_zap.c +++ b/channels/chan_zap.c @@ -1659,17 +1659,15 @@ int send_cwcidspill(struct zt_pvt *p) { p->callwaitcas = 0; p->cidcwexpire = 0; - p->cidspill = malloc(MAX_CALLERID_SIZE); - if (p->cidspill) { - memset(p->cidspill, 0x7f, MAX_CALLERID_SIZE); - p->cidlen = ast_callerid_callwaiting_generate(p->cidspill, p->callwait_name, p->callwait_num, AST_LAW(p)); - /* Make sure we account for the end */ - p->cidlen += READ_SIZE * 4; - p->cidpos = 0; - send_callerid(p); - if (option_verbose > 2) - ast_verbose(VERBOSE_PREFIX_3 "CPE supports Call Waiting Caller*ID. Sending '%s/%s'\n", p->callwait_name, p->callwait_num); - } else return -1; + if (!(p->cidspill = ast_malloc(MAX_CALLERID_SIZE))) + return -1; + p->cidlen = ast_callerid_callwaiting_generate(p->cidspill, p->callwait_name, p->callwait_num, AST_LAW(p)); + /* Make sure we account for the end */ + p->cidlen += READ_SIZE * 4; + p->cidpos = 0; + send_callerid(p); + if (option_verbose > 2) + ast_verbose(VERBOSE_PREFIX_3 "CPE supports Call Waiting Caller*ID. Sending '%s/%s'\n", p->callwait_name, p->callwait_num); return 0; } @@ -1720,26 +1718,23 @@ static int zt_callwait(struct ast_channel *ast) ast_log(LOG_WARNING, "Spill already exists?!?\n"); free(p->cidspill); } - p->cidspill = malloc(2400 /* SAS */ + 680 /* CAS */ + READ_SIZE * 4); - if (p->cidspill) { - save_conference(p); - /* Silence */ - memset(p->cidspill, 0x7f, 2400 + 600 + READ_SIZE * 4); - if (!p->callwaitrings && p->callwaitingcallerid) { - ast_gen_cas(p->cidspill, 1, 2400 + 680, AST_LAW(p)); - p->callwaitcas = 1; - p->cidlen = 2400 + 680 + READ_SIZE * 4; - } else { - ast_gen_cas(p->cidspill, 1, 2400, AST_LAW(p)); - p->callwaitcas = 0; - p->cidlen = 2400 + READ_SIZE * 4; - } - p->cidpos = 0; - send_callerid(p); - } else { - ast_log(LOG_WARNING, "Unable to create SAS/CAS spill\n"); + if (!(p->cidspill = ast_malloc(2400 /* SAS */ + 680 /* CAS */ + READ_SIZE * 4))) return -1; + save_conference(p); + /* Silence */ + memset(p->cidspill, 0x7f, 2400 + 600 + READ_SIZE * 4); + if (!p->callwaitrings && p->callwaitingcallerid) { + ast_gen_cas(p->cidspill, 1, 2400 + 680, AST_LAW(p)); + p->callwaitcas = 1; + p->cidlen = 2400 + 680 + READ_SIZE * 4; + } else { + ast_gen_cas(p->cidspill, 1, 2400, AST_LAW(p)); + p->callwaitcas = 0; + p->cidlen = 2400 + READ_SIZE * 4; } + p->cidpos = 0; + send_callerid(p); + return 0; } @@ -1796,14 +1791,12 @@ static int zt_call(struct ast_channel *ast, char *rdest, int timeout) ast_log(LOG_WARNING, "cidspill already exists??\n"); free(p->cidspill); } - p->cidspill = malloc(MAX_CALLERID_SIZE); p->callwaitcas = 0; - if (p->cidspill) { + if ((p->cidspill = ast_malloc(MAX_CALLERID_SIZE))) { p->cidlen = ast_callerid_generate(p->cidspill, ast->cid.cid_name, ast->cid.cid_num, AST_LAW(p)); p->cidpos = 0; send_callerid(p); - } else - ast_log(LOG_WARNING, "Unable to generate CallerID spill\n"); + } } /* Choose proper cadence */ if ((p->distinctivering > 0) && (p->distinctivering <= num_cadence)) { @@ -4082,9 +4075,9 @@ static struct ast_frame *zt_handle_event(struct ast_channel *ast) chan = zt_new(p, AST_STATE_RESERVED, 0, SUB_THREEWAY, 0, 0); if (p->zaptrcallerid) { if (!p->origcid_num) - p->origcid_num = strdup(p->cid_num); + p->origcid_num = ast_strdup(p->cid_num); if (!p->origcid_name) - p->origcid_name = strdup(p->cid_name); + p->origcid_name = ast_strdup(p->cid_name); ast_copy_string(p->cid_num, cid_num, sizeof(p->cid_num)); ast_copy_string(p->cid_name, cid_name, sizeof(p->cid_name)); } @@ -5160,9 +5153,9 @@ static struct ast_channel *zt_new(struct zt_pvt *i, int state, int startpbx, int if (!ast_strlen_zero(i->exten)) ast_copy_string(tmp->exten, i->exten, sizeof(tmp->exten)); if (!ast_strlen_zero(i->rdnis)) - tmp->cid.cid_rdnis = strdup(i->rdnis); + tmp->cid.cid_rdnis = ast_strdup(i->rdnis); if (!ast_strlen_zero(i->dnid)) - tmp->cid.cid_dnid = strdup(i->dnid); + tmp->cid.cid_dnid = ast_strdup(i->dnid); #ifdef PRI_ANI ast_set_callerid(tmp, i->cid_num, i->cid_name, ast_strlen_zero(i->cid_ani) ? i->cid_num : i->cid_ani); @@ -6571,9 +6564,7 @@ static void *do_monitor(void *data) if (pfds) free(pfds); if (ifcount) { - pfds = malloc(ifcount * sizeof(struct pollfd)); - if (!pfds) { - ast_log(LOG_WARNING, "Critical memory error. Zap dies.\n"); + if (!(pfds = ast_calloc(1, ifcount * sizeof(*pfds)))) { ast_mutex_unlock(&iflock); return NULL; } @@ -6648,8 +6639,7 @@ static void *do_monitor(void *data) res2 = ioctl(last->subs[SUB_REAL].zfd, ZT_FLUSH, &x); if (res2) ast_log(LOG_WARNING, "Unable to flush input on channel %d\n", last->channel); - last->cidspill = malloc(MAX_CALLERID_SIZE); - if (last->cidspill) { + if ((last->cidspill = ast_calloc(1, MAX_CALLERID_SIZE))) { /* Turn on on hook transfer for 4 seconds */ x = 4000; ioctl(last->subs[SUB_REAL].zfd, ZT_ONHOOKTRANSFER, &x); @@ -6976,13 +6966,10 @@ static struct zt_pvt *mkintf(int channel, int signalling, int radio, struct zt_p } if (!here && !reloading) { - tmp = (struct zt_pvt*)malloc(sizeof(struct zt_pvt)); - if (!tmp) { - ast_log(LOG_ERROR, "MALLOC FAILED\n"); + if (!(tmp = ast_calloc(1, sizeof(*tmp)))) { destroy_zt_pvt(&tmp); return NULL; } - memset(tmp, 0, sizeof(struct zt_pvt)); ast_mutex_init(&tmp->lock); ifcount++; for (x=0;x<3;x++) @@ -7509,8 +7496,8 @@ static struct zt_pvt *chandup(struct zt_pvt *src) struct zt_pvt *p; ZT_BUFFERINFO bi; int res; - p = malloc(sizeof(struct zt_pvt)); - if (p) { + + if ((p = ast_malloc(sizeof(*p)))) { memcpy(p, src, sizeof(struct zt_pvt)); ast_mutex_init(&p->lock); p->subs[SUB_REAL].zfd = zt_open("/dev/zap/pseudo"); @@ -9271,7 +9258,7 @@ static char *complete_span_helper(char *line, char *word, int pos, int state, in } if (span <= NUM_SPANS) { snprintf(tmp, sizeof(tmp), "%d", span); - return strdup(tmp); + return ast_strdup(tmp); } else return NULL; } @@ -11096,13 +11083,11 @@ static int zt_sendtext(struct ast_channel *c, const char *text) if (!text[0]) return(0); /* if nothing to send, dont */ if ((!p->tdd) && (!p->mate)) return(0); /* if not in TDD mode, just return */ if (p->mate) - buf = malloc(((strlen(text) + 1) * ASCII_BYTES_PER_CHAR) + END_SILENCE_LEN + HEADER_LEN); + buf = ast_malloc(((strlen(text) + 1) * ASCII_BYTES_PER_CHAR) + END_SILENCE_LEN + HEADER_LEN); else - buf = malloc(((strlen(text) + 1) * TDD_BYTES_PER_CHAR) + END_SILENCE_LEN); - if (!buf) { - ast_log(LOG_ERROR, "MALLOC FAILED\n"); + buf = ast_malloc(((strlen(text) + 1) * TDD_BYTES_PER_CHAR) + END_SILENCE_LEN); + if (!buf) return -1; - } mybuf = buf; if (p->mate) { int codec = AST_LAW(p); -- cgit v1.2.3