]> git.ipfire.org Git - thirdparty/openssl.git/commit
hashtable: pass HT into hash function
authorNikola Pajkovsky <nikolap@openssl.org>
Wed, 24 Sep 2025 15:12:39 +0000 (17:12 +0200)
committerNeil Horman <nhorman@openssl.org>
Thu, 2 Oct 2025 12:04:47 +0000 (08:04 -0400)
commit18763ea15507ecd3cb24d717fd6e738223272391
tree40c31d0757c9ca9511a1aec9d9527f7411795565
parent6387ec6d492caffa4c9bc137f1cb6c171366c7c7
hashtable: pass HT into hash function

When defining a custom hash function for a hashtable key, you typically start with:

  HT_START_KEY_DEFN(key)
  HT_DEF_KEY_FIELD(k, unsigned char *)
  HT_END_KEY_DEFN(KEY)

In this setup, the hash function signature requires keybuf and len as
parameters rather than the hashtable key itself. As a result,
accessing members of the hashtable structure becomes awkward, since
you must do something like:

  #define FROM_KEYBUF_TO_HT_KEY(keybuf, type) (type)((keybuf) - sizeof(HT_KEY))

  static uint64_t ht_hash(uint8_t *keybuf, size_t keylen)
  {
      KEY *k = FROM_KEYBUF_TO_HT_KEY(keybuf, KEY *);
      ...
  }

This kind of pointer arithmetic is both unnecessary and error-prone.
A cleaner approach is to pass the HT pointer directly into the hash
function. From there, you can safely cast it to the required type
without the pointer gymnastics.

Signed-off-by: Nikola Pajkovsky <nikolap@openssl.org>
Reviewed-by: Saša Nedvědický <sashan@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Neil Horman <nhorman@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/28677)
crypto/hashtable/hashtable.c
include/internal/hashtable.h
test/lhash_test.c