浏览代码

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
Daniel-Constantin Mierla 6 年之前
父节点
当前提交
087654a502
共有 1 个文件被更改,包括 9 次插入23 次删除
  1. 9 23
      src/modules/http_async_client/hm_hash.c

+ 9 - 23
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;
 
 }