Browse code

topoh: improve checks for via header processing

(cherry picked from commit 78ccdb7ff1ebc34748092fe01ff249f5eeb661c8)

Henning Westerholt authored on 24/02/2019 20:25:25 • Henning Westerholt committed on 03/04/2019 21:24:45
Showing 2 changed files
... ...
@@ -141,6 +141,12 @@ char* th_mask_decode(char *in, int ilen, str *prefix, int extra, int *olen)
141 141
 
142 142
 	*olen = (((ilen-((prefix!=NULL&&prefix->len>0)?prefix->len:0)) * 6) >> 3)
143 143
 				- n;
144
+
145
+	if (*olen<=0) {
146
+		LM_ERR("invalid olen parameter calculated, can't continue %d\n", *olen);
147
+		return NULL;
148
+	}
149
+
144 150
 	out = (char*)pkg_malloc((*olen+1+extra)*sizeof(char));
145 151
 
146 152
 	if(out==NULL)
... ...
@@ -411,7 +411,7 @@ int th_unmask_via(sip_msg_t *msg, str *cookie)
411 411
 				else
412 412
 					out.s = th_mask_decode(vp->value.s, vp->value.len,
413 413
 							&th_vparam_prefix, 0, &out.len);
414
-				if(out.s==NULL)
414
+				if(out.s==NULL || out.len<=0)
415 415
 				{
416 416
 					LM_ERR("cannot decode via %d\n", i);
417 417
 					return -1;
... ...
@@ -945,7 +945,12 @@ int th_add_via_cookie(sip_msg_t *msg, struct via_body *via)
945 945
 	if (via->params.s) {
946 946
 		viap = via->params.s - via->hdr.s - 1;
947 947
 	} else {
948
-		viap = via->host.s - via->hdr.s + via->host.len;
948
+		if (via->host.s) {
949
+			viap = via->host.s - via->hdr.s + via->host.len;
950
+		} else {
951
+			LM_ERR("no via parameter and no via host, can't insert cookie\n");
952
+			return -1;
953
+		}
949 954
 		if (via->port!=0)
950 955
 			viap += via->port_str.len + 1; /* +1 for ':'*/
951 956
 	}