From b288766b85260ce6e9974a0ec590336b2ab82dd5 Mon Sep 17 00:00:00 2001 From: Russell Bryant Date: Thu, 6 Dec 2007 22:16:32 +0000 Subject: [PATCH] Change malloc+memset to calloc. Also, handle allocation failures early to reduce indentation. (closes issue #11469) Reported by: eliel Patches: pri.c.patch uploaded by eliel (license 64) q931.c.patch uploaded by eliel (license 64) q921.c.patch uploaded by eliel (license 64) pri_facility.c.patch uploaded by eliel (license 64) git-svn-id: https://origsvn.digium.com/svn/libpri/trunk@496 2fbb986a-6c06-0410-b554-c9c1f0a7f128 --- pri.c | 2 +- pri_facility.c | 17 +++++++---------- q921.c | 37 ++++++++++++++++--------------------- q931.c | 42 ++++++++++++++++++------------------------ 4 files changed, 42 insertions(+), 56 deletions(-) diff --git a/pri.c b/pri.c index 01d28ba..d331fd5 100644 --- a/pri.c +++ b/pri.c @@ -845,7 +845,7 @@ void pri_enslave(struct pri *master, struct pri *slave) struct pri_sr *pri_sr_new(void) { struct pri_sr *req; - req = malloc(sizeof(struct pri_sr)); + req = malloc(sizeof(*req)); if (req) pri_sr_init(req); return req; diff --git a/pri_facility.c b/pri_facility.c index 3e03d85..bd3bb42 100644 --- a/pri_facility.c +++ b/pri_facility.c @@ -2344,19 +2344,16 @@ int pri_call_apdu_queue(q931_call *call, int messagetype, void *apdu, int apdu_l if (!call || !messagetype || !apdu || (apdu_len < 1) || (apdu_len > 255)) return -1; - new_event = malloc(sizeof(struct apdu_event)); - - if (new_event) { - memset(new_event, 0, sizeof(struct apdu_event)); - new_event->message = messagetype; - new_event->callback = function; - new_event->data = data; - memcpy(new_event->apdu, apdu, apdu_len); - new_event->apdu_len = apdu_len; - } else { + if (!(new_event = calloc(1, sizeof(*new_event)))) { pri_error(call->pri, "!! Malloc failed!\n"); return -1; } + + new_event->message = messagetype; + new_event->callback = function; + new_event->data = data; + memcpy(new_event->apdu, apdu, apdu_len); + new_event->apdu_len = apdu_len; if (call->apdus) { cur = call->apdus; diff --git a/q921.c b/q921.c index b17ddc0..5268be8 100644 --- a/q921.c +++ b/q921.c @@ -99,25 +99,21 @@ static void q921_send_tei(struct pri *pri, int message, int ri, int ai, int isre { q921_u *f; - f = malloc(sizeof(*f) + 5); - if (f) { - memset(f, 0, sizeof(*f) + 5); - Q921_INIT(pri, *f); - f->h.c_r = isreq; - f->m3 = 0; - f->m2 = 0; - f->p_f = 0; - f->ft = Q921_FRAMETYPE_U; - f->data[0] = 0x0f; /* Management entity */ - f->data[1] = (ri >> 8) & 0xff; - f->data[2] = ri & 0xff; - f->data[3] = message; - f->data[4] = (ai << 1) | 1; -// if (pri->debug & PRI_DEBUG_Q921_STATE) - pri_message(pri, "Sending TEI management message %d, TEI=%d\n", message, ai); - q921_transmit(pri, (q921_h *)f, 8); - free(f); - } + if (!(f = calloc(1, sizeof(*f) + 5))) + return; + + Q921_INIT(pri, *f); + f->h.c_r = isreq; + f->ft = Q921_FRAMETYPE_U; + f->data[0] = 0x0f; /* Management entity */ + f->data[1] = (ri >> 8) & 0xff; + f->data[2] = ri & 0xff; + f->data[3] = message; + f->data[4] = (ai << 1) | 1; +// if (pri->debug & PRI_DEBUG_Q921_STATE) + pri_message(pri, "Sending TEI management message %d, TEI=%d\n", message, ai); + q921_transmit(pri, (q921_h *)f, 8); + free(f); } static void q921_tei_request(void *vpri) @@ -440,9 +436,8 @@ int q921_transmit_iframe(struct pri *pri, void *buf, int len, int cr) q921_frame *f, *prev=NULL; for (f=pri->txqueue; f; f = f->next) prev = f; - f = malloc(sizeof(q921_frame) + len + 2); + f = calloc(1, sizeof(q921_frame) + len + 2); if (f) { - memset(f,0,sizeof(q921_frame) + len + 2); Q921_INIT(pri, f->h); switch(pri->localtype) { case PRI_NETWORK: diff --git a/q931.c b/q931.c index 0e8d9b4..500ce08 100644 --- a/q931.c +++ b/q931.c @@ -255,18 +255,10 @@ static char *code2str(int code, struct msgtype *codes, int max) static void call_init(struct q931_call *c) { - memset(c, 0, sizeof(*c)); - c->alive = 0; - c->sendhangupack = 0; c->forceinvert = -1; c->cr = -1; c->slotmap = -1; c->channelno = -1; - c->ds1no = 0; - c->ds1explicit = 0; - c->chanflags = 0; - c->next = NULL; - c->sentchannel = 0; c->newcall = 1; c->ourcallstate = Q931_CALL_STATE_NULL; c->peercallstate = Q931_CALL_STATE_NULL; @@ -2196,23 +2188,25 @@ static q931_call *q931_getcall(struct pri *pri, int cr, int outboundnew) /* No call exists, make a new one */ if (pri->debug & PRI_DEBUG_Q931_STATE) pri_message(pri, "-- Making new call for cr %d\n", cr); - cur = malloc(sizeof(struct q931_call)); - if (cur) { - call_init(cur); - /* Call reference */ - cur->cr = cr; - /* PRI is set to whoever called us */ - if (pri->bri && (pri->localtype == PRI_CPE) && pri->subchannel && outboundnew) - cur->pri = pri->subchannel; - else - cur->pri = pri; + + if (!(cur = calloc(1, sizeof(*cur)))) + return NULL; - /* Append to end of list */ - if (prev) - prev->next = cur; - else - *master->callpool = cur; - } + call_init(cur); + /* Call reference */ + cur->cr = cr; + /* PRI is set to whoever called us */ + if (pri->bri && (pri->localtype == PRI_CPE) && pri->subchannel && outboundnew) + cur->pri = pri->subchannel; + else + cur->pri = pri; + + /* Append to end of list */ + if (prev) + prev->next = cur; + else + *master->callpool = cur; + return cur; }