[Asterisk-cvs] asterisk/channels chan_sip.c,1.722,1.723

kpfleming at lists.digium.com kpfleming at lists.digium.com
Tue May 3 19:44:15 CDT 2005


Update of /usr/cvsroot/asterisk/channels
In directory mongoose.digium.com:/tmp/cvs-serv10309/channels

Modified Files:
	chan_sip.c 
Log Message:
fix breakage when ast_copy_string is used to copy substrings (bug #4146, but a different fix)


Index: chan_sip.c
===================================================================
RCS file: /usr/cvsroot/asterisk/channels/chan_sip.c,v
retrieving revision 1.722
retrieving revision 1.723
diff -u -d -r1.722 -r1.723
--- chan_sip.c	3 May 2005 03:22:14 -0000	1.722
+++ chan_sip.c	3 May 2005 23:51:20 -0000	1.723
@@ -1492,7 +1492,7 @@
 		ast_copy_string(r->fullcontact, p->fullcontact, sizeof(r->fullcontact));
 		if (!r->initreq.headers && !ast_strlen_zero(p->fromdomain)) {
 			if ((callhost = strchr(r->callid, '@'))) {
-				ast_copy_string(callhost + 1, p->fromdomain, sizeof(r->callid) - (callhost - r->callid) - 1);
+				strncpy(callhost + 1, p->fromdomain, sizeof(r->callid) - (callhost - r->callid) - 2);
 			}
 		}
 		if (ast_strlen_zero(r->tohost)) {
@@ -3243,7 +3243,7 @@
 static void add_route(struct sip_request *req, struct sip_route *route)
 {
 	char r[256], *p;
-	int n, rem = 255; /* sizeof(r)-1: Room for terminating 0 */
+	int n, rem = sizeof(r);
 
 	if (!route) return;
 
@@ -3292,10 +3292,10 @@
 		else if (strncmp(h, "sips:", 5) == 0)
 			h += 5;
 	}
-	hn = strcspn(h, ":;>");
-	if (hn > (sizeof(hostname) - 1)) hn = sizeof(hostname) - 1;
+	hn = strcspn(h, ":;>") + 1;
+	if (hn > sizeof(hostname)) hn = sizeof(hostname);
 	ast_copy_string(hostname, h, hn);
-	h+=hn;
+	h += hn - 1;
 
 	/* Is "port" present? if not default to 5060 */
 	if (*h == ':') {
@@ -3310,9 +3310,9 @@
 	maddr = strstr(h, "maddr=");
 	if (maddr) {
 		maddr += 6;
-		hn = strspn(maddr, "0123456789.");
-		if (hn > (sizeof(hostname) - 1)) hn = sizeof(hostname) - 1;
-		ast_copy_string(hostname, maddr, hn);
+		hn = strspn(maddr, "0123456789.") + 1;
+		if (hn > sizeof(hostname)) hn = sizeof(hostname);
+		ast_copy_string(hostname, h, hn);
 	}
 	
 	hp = ast_gethostbyname(hostname, &ahp);
@@ -5073,9 +5073,9 @@
 			rr = strchr(rr, '<');
 			if (!rr) break; /* No more hops */
 			++rr;
-			len = strcspn(rr, ">");
+			len = strcspn(rr, ">") + 1;
 			/* Make a struct route */
-			thishop = (struct sip_route *)malloc(sizeof(struct sip_route)+len+1);
+			thishop = malloc(sizeof(*thishop) + len);
 			if (thishop) {
 				ast_copy_string(thishop->hop, rr, len);
 				ast_log(LOG_DEBUG, "build_route: Record-Route hop: <%s>\n", thishop->hop);
@@ -5096,7 +5096,7 @@
 					tail = thishop;
 				}
 			}
-			rr += len+1;
+			rr += len;
 		}
 	}
 	/* 2nd append the Contact: if there is one */
@@ -5109,12 +5109,13 @@
 		if (c) {
 			/* Take to > */
 			++c;
-			len = strcspn(c, ">");
+			len = strcspn(c, ">") + 1;
 		} else {
 			/* No <> - just take the lot */
-			c = contact; len = strlen(contact);
+			c = contact;
+			len = strlen(contact) + 1;
 		}
-		thishop = (struct sip_route *)malloc(sizeof(struct sip_route)+len+1);
+		thishop = malloc(sizeof(*thishop) + len);
 		if (thishop) {
 			ast_copy_string(thishop->hop, c, len);
 			thishop->next = NULL;
@@ -5758,7 +5759,7 @@
 }
 
 /*--- get_calleridname: Get caller id name from SIP headers ---*/
-static char *get_calleridname(char *input,char *output, size_t outputsize)
+static char *get_calleridname(char *input, char *output, size_t outputsize)
 {
 	char *end = strchr(input,'<');
 	char *tmp = strchr(input,'\"');
@@ -5770,15 +5771,13 @@
 	end--;
 	/* we found "name" */
 	if (tmp && tmp < end) {
-		end = strchr(tmp+1,'\"');
+		end = strchr(tmp+1, '\"');
 		if (!end) return NULL;
-		bytes = (int)(end-tmp-1);
+		bytes = (int) (end - tmp);
 		/* protect the output buffer */
-		if (bytes > maxbytes) {
+		if (bytes > maxbytes)
 			bytes = maxbytes;
-		}
-		ast_copy_string(output, tmp+1, bytes); /* safe */
-		output[maxbytes] = '\0';
+		ast_copy_string(output, tmp + 1, bytes);
 	} else {
 		/* we didn't find "name" */
 		/* clear the empty characters in the begining*/
@@ -5788,13 +5787,12 @@
 		while(*end && (*end < 33) && end > input)
 			end--;
 		if (end >= input) {
-			bytes = (int)(end-input)+1;
+			bytes = (int) (end - input) + 2;
 			/* protect the output buffer */
 			if (bytes > maxbytes) {
 				bytes = maxbytes;
 			}
-			ast_copy_string(output, input, bytes); /* safe */
-			output[maxbytes] = '\0';
+			ast_copy_string(output, input, bytes);
 		}
 		else
 			return(NULL);
@@ -7819,7 +7817,6 @@
 	}
 
 	ast_copy_string(buf, content, len);
-	buf[len-1] = '\0';
 	ast_mutex_unlock(&chan->lock);
 
 	return buf;




More information about the svn-commits mailing list