[sr-dev] git:master:087654a5: http_async_client: use enough size to print pointer as string in build_hash_key()

Daniel-Constantin Mierla miconda at gmail.com
Mon Oct 7 16:28:44 CEST 2019


Module: kamailio
Branch: master
Commit: 087654a5028cd800e17fcd9d1768135a60fd6706
URL: https://github.com/kamailio/kamailio/commit/087654a5028cd800e17fcd9d1768135a60fd6706

Author: Daniel-Constantin Mierla <miconda at gmail.com>
Committer: Daniel-Constantin Mierla <miconda at gmail.com>
Date: 2019-10-07T16:28:24+02:00

http_async_client: use enough size to print pointer as string in build_hash_key()

- use local string variables instead of allocation in pkg, because the values
are needed only inside the function
- use safer snprintf() instead of sprintf()
- GH #2091

---

Modified: src/modules/http_async_client/hm_hash.c

---

Diff:  https://github.com/kamailio/kamailio/commit/087654a5028cd800e17fcd9d1768135a60fd6706.diff
Patch: https://github.com/kamailio/kamailio/commit/087654a5028cd800e17fcd9d1768135a60fd6706.patch

---

diff --git a/src/modules/http_async_client/hm_hash.c b/src/modules/http_async_client/hm_hash.c
index 9497809847..b18a8b9c37 100644
--- a/src/modules/http_async_client/hm_hash.c
+++ b/src/modules/http_async_client/hm_hash.c
@@ -62,39 +62,25 @@ int init_http_m_table(unsigned int size)
 
 unsigned int build_hash_key(void *p)
 {
-	str			*hash_str;
-	char		*pointer_str;
-	int			len;
+	str			hash_str;
+	char		pointer_str[20];
 
 	unsigned int hash;
 
-	pointer_str = (char *)pkg_malloc(sizeof(p) + 1);
-
-	if (pointer_str==0) {
-		LM_ERR("no more pkg mem\n");
+	hash_str.len = snprintf(pointer_str, 20, "%p", p);
+	if(hash_str.len<=0 || hash_str.len>=20) {
+		LM_ERR("failed to print the pointer address\n");
 		return 0;
 	}
+	LM_DBG("received id %p (%d)-> %s (%d)\n", p, (int)sizeof(p), pointer_str,
+			hash_str.len);
 
-	sprintf(pointer_str, "%p", p);
-	len = strlen(pointer_str);
-	LM_DBG("received id %p (%d)-> %s (%d)\n", p, (int)sizeof(p), pointer_str, len);
+	hash_str.s = pointer_str;
 
-	hash_str = (str *)pkg_malloc(sizeof(str));
-	if (hash_str==0) {
-		LM_ERR("no more pkg mem\n");
-		pkg_free(pointer_str);
-		return 0;
-	}
-	hash_str->s = pointer_str;
-	hash_str->len = len;
-
-	hash = core_hash(hash_str, 0, hash_size);
+	hash = core_hash(&hash_str, 0, hash_size);
 
 	LM_DBG("hash for %p is %d\n", p, hash);
 
-	pkg_free(pointer_str);
-	pkg_free(hash_str);
-
 	return hash;
 
 }




More information about the sr-dev mailing list