Browse code

pv: use unsigned for safer non-ascii bit shifting for hexa

- related to GH #2690

Daniel-Constantin Mierla authored on 27/03/2021 13:43:05
Showing 2 changed files
... ...
@@ -615,20 +615,22 @@ int pv_get_flag(struct sip_msg *msg, pv_param_t *param,
615 615
 	return pv_get_uintval(msg, param, res, (msg->flags & (1<<param->pvn.u.isname.name.n)) ? 1 : 0);
616 616
 }
617 617
 
618
-static inline char* int_to_8hex(int val)
618
+static inline char* int_to_8hex(int sval)
619 619
 {
620 620
 	unsigned short digit;
621 621
 	int i;
622 622
 	static char outbuf[9];
623
+	unsigned int uval;
623 624
 
625
+	uval = (unsigned int)sval;
624 626
 	outbuf[8] = '\0';
625 627
 	for(i=0; i<8; i++)
626 628
 	{
627
-		if(val!=0)
629
+		if(uval!=0)
628 630
 		{
629
-			digit =  val & 0x0f;
631
+			digit =  uval & 0x0f;
630 632
 			outbuf[7-i] = digit >= 10 ? digit + 'a' - 10 : digit + '0';
631
-			val >>= 4;
633
+			uval >>= 4;
632 634
 		}
633 635
 		else
634 636
 			outbuf[7-i] = '0';
... ...
@@ -118,7 +118,7 @@ static int pdu_7bit_encode(str sin) {
118 118
 	nleft = 1;
119 119
 	j = 0;
120 120
 	for(i = 0; i < sin.len; i++) {
121
-		hex = *(sin.s) >> (nleft - 1);
121
+		hex = (unsigned char)(*(sin.s)) >> (nleft - 1);
122 122
 		fill = *(sin.s+1) << (8-nleft);
123 123
 		hex = hex | fill;
124 124
 		_tr_buffer[j++] = HexTbl[hex >> 4];
... ...
@@ -292,7 +292,7 @@ int tr_eval_string(struct sip_msg *msg, tr_param_t *tp, int subtype,
292 292
 			j = 0;
293 293
 			for(i=0; i<val->rs.len; i++)
294 294
 			{
295
-				_tr_buffer[j++] = fourbits2char[val->rs.s[i] >> 4];
295
+				_tr_buffer[j++] = fourbits2char[(unsigned char)val->rs.s[i] >> 4];
296 296
 				_tr_buffer[j++] = fourbits2char[val->rs.s[i] & 0xf];
297 297
 			}
298 298
 			_tr_buffer[j] = '\0';