[libpri-commits] russell: trunk r496 - in /trunk: pri.c pri_facility.c q921.c q931.c

SVN commits to the libpri project libpri-commits at lists.digium.com
Thu Dec 6 16:16:32 CST 2007


Author: russell
Date: Thu Dec  6 16:16:32 2007
New Revision: 496

URL: http://svn.digium.com/view/libpri?view=rev&rev=496
Log:
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)

Modified:
    trunk/pri.c
    trunk/pri_facility.c
    trunk/q921.c
    trunk/q931.c

Modified: trunk/pri.c
URL: http://svn.digium.com/view/libpri/trunk/pri.c?view=diff&rev=496&r1=495&r2=496
==============================================================================
--- trunk/pri.c (original)
+++ trunk/pri.c Thu Dec  6 16:16:32 2007
@@ -845,7 +845,7 @@
 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;

Modified: trunk/pri_facility.c
URL: http://svn.digium.com/view/libpri/trunk/pri_facility.c?view=diff&rev=496&r1=495&r2=496
==============================================================================
--- trunk/pri_facility.c (original)
+++ trunk/pri_facility.c Thu Dec  6 16:16:32 2007
@@ -2344,19 +2344,16 @@
 	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;

Modified: trunk/q921.c
URL: http://svn.digium.com/view/libpri/trunk/q921.c?view=diff&rev=496&r1=495&r2=496
==============================================================================
--- trunk/q921.c (original)
+++ trunk/q921.c Thu Dec  6 16:16:32 2007
@@ -99,25 +99,21 @@
 {
 	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 @@
 	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:

Modified: trunk/q931.c
URL: http://svn.digium.com/view/libpri/trunk/q931.c?view=diff&rev=496&r1=495&r2=496
==============================================================================
--- trunk/q931.c (original)
+++ trunk/q931.c Thu Dec  6 16:16:32 2007
@@ -255,18 +255,10 @@
 
 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 @@
 	/* 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;
-
-		/* Append to end of list */
-		if (prev)
-			prev->next = cur;
-		else
-			*master->callpool = cur;
-	}
+	
+	if (!(cur = calloc(1, sizeof(*cur))))
+		return NULL;
+
+	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;
 }
 




More information about the libpri-commits mailing list