From d08d16cac33f2d53269888dd2414c495f2a5d53e Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Thu, 26 Sep 2024 16:05:20 +0200 Subject: [PATCH] multi: avoid reading whole struct pointer from pointer The proper alignment is not guaranteed. This function now instead uses only the first and last byte of the key since they are the ones likely to change most (one of them, depending on CPU endian) and the hash is tiny anyway. Closes #15063 --- lib/multi.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/multi.c b/lib/multi.c index 27b8ef3075..413f4329fb 100644 --- a/lib/multi.c +++ b/lib/multi.c @@ -240,12 +240,13 @@ static struct Curl_sh_entry *sh_getentry(struct Curl_hash *sh, } #define TRHASH_SIZE 13 + +/* the given key here is a struct Curl_easy pointer */ static size_t trhash(void *key, size_t key_length, size_t slots_num) { - size_t keyval = (size_t)*(struct Curl_easy **)key; - (void) key_length; - - return (keyval % slots_num); + unsigned char bytes = ((unsigned char *)key)[key_length - 1] ^ + ((unsigned char *)key)[0]; + return (bytes % slots_num); } static size_t trhash_compare(void *k1, size_t k1_len, void *k2, size_t k2_len) -- 2.47.3