Blob Blame History Raw
From: Bogdan-Andrei Iancu <bogdan@opensips.org>
Date: Tue, 30 Jan 2018 15:02:47 +0200
Subject: [PATCH] Fix reverse_hex2int64() prototype

As the computed value is an unsigned (as data size), it cannot be returned as int as it will overflow and get converted to a negative value. This will colide with the negative error ret code -1

(cherry picked from commit ed12eb97c06a23491c8b831124600361b0b63f2f)

diff --git a/modules/nathelper/sip_pinger.h b/modules/nathelper/sip_pinger.h
index ceee556fd..94e98d5a8 100644
--- a/modules/nathelper/sip_pinger.h
+++ b/modules/nathelper/sip_pinger.h
@@ -97,8 +97,6 @@ static int parse_branch(str branch)
 	unsigned int hash_id;
 	int cid_len;
 	char *end;
-
-	int64_t ret;
 	uint64_t contact_id=0;
 
 	struct ping_cell *p_cell;
@@ -133,15 +131,12 @@ static int parse_branch(str branch)
 
 	end = q_memchr(branch.s, '.', branch.len);
 	cid_len = end-branch.s;
-	ret = reverse_hex2int64(branch.s, cid_len, 1/* request unsafe parsing */);
-	/* we don't parse the label since we don't need it */
-
-	if (ret == -1) {
-		LM_ERR("received invalid contact id\n");
-		return -1;
-	}
+	reverse_hex2int64(branch.s, cid_len, 1/* request unsafe parsing */,
+		&contact_id);
+	/* reverse_hex2int64() cannot fail in unsafe mode and it will return 
+	   whatever it was able to parse (0 if nothing )*/
 
-	contact_id = (uint64_t)ret;
+	/* we don't parse the label since we don't need it */
 
 	lock_hash(hash_id);
 	if ((p_cell=get_cell(hash_id, contact_id))==NULL) {
diff --git a/ut.h b/ut.h
index 744fd07e7..c7af16bc0 100644
--- a/ut.h
+++ b/ut.h
@@ -280,24 +280,23 @@ inline static int int2reverse_hex( char **c, int *size, unsigned int nr )
 /* if unsafe requested when first non numerical character shall be
  * met the number shall be returned; avoid giving the
  * exact len of the number */
-inline static int64_t reverse_hex2int64( char *c, int len, int unsafe)
+inline static int reverse_hex2int64( char *c, int len, int unsafe, uint64_t *r)
 {
 	char *pc;
-	int64_t r;
 	char mychar;
 
-	r=0;
+	*r=0;
 	for (pc=c+len-1; len>0; pc--, len--) {
-		r <<= 4 ;
+		(*r) <<= 4 ;
 		mychar=*pc;
-		if ( mychar >='0' && mychar <='9') r+=mychar -'0';
-		else if (mychar >='a' && mychar <='f') r+=mychar -'a'+10;
-		else if (mychar  >='A' && mychar <='F') r+=mychar -'A'+10;
+		if ( mychar >='0' && mychar <='9') (*r)+=mychar -'0';
+		else if (mychar >='a' && mychar <='f') (*r)+=mychar -'a'+10;
+		else if (mychar  >='A' && mychar <='F') (*r)+=mychar -'A'+10;
 		else if (unsafe)
-			return r;
+			return 0;
 		else return -1;
 	}
-	return r;
+	return 0;
 }
 
 inline static int64_t int64_2reverse_hex( char **c, int *size, uint64_t nr )