util/hash_table: Don't leak hash_u64_key objects when the entry exists
authorBoris Brezillon <boris.brezillon@collabora.com>
Fri, 1 Dec 2023 18:12:08 +0000 (19:12 +0100)
committerEric Engestrom <eric@engestrom.ch>
Sun, 17 Dec 2023 23:48:00 +0000 (23:48 +0000)
commite2427f915cbab0f20cd6b145bb8b38b15277245d
treed7f77fe2d272e0eb9102a9bcb916b2208d3b7770
parentb02dc14ff713050493ef8b49f119f8a4b4d34dfd
util/hash_table: Don't leak hash_u64_key objects when the entry exists

When an entry exists, _mesa_hash_table_insert() updates the entry with
the new data/key pair, which causes a leak if the key has previously
been dynamically allocated, like is the case for hash_u64_key keys.

One solution to solve that is to do the insertion in two steps: first
_mesa_hash_table_search_pre_hashed(), and if the entry doesn't exist
_mesa_hash_table_insert_pre_hashed(). But approach forces us to do the
double-hashing twice.

Another approach is to extract the logic in hash_table_insert() that's
responsible for the searching and entry allocation into a separate helper
called hash_table_get_entry(), and keep the entry::{key,data} assignment
in hash_table_insert().

This way we can re-use hash_table_get_entry() from
_mesa_hash_table_u64_insert(), and lake sure we free the allocated
key if the entry was already present.

Fixes: 6649b840c340 ("mesa/util: add a hash table wrapper which support 64-bit keys")
Cc: stable
Signed-off-by: Boris Brezillon <boris.brezillon@collabora.com>
Reviewed-by: Yonggang Luo <luoyonggang@gmail.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/26423>
(cherry picked from commit 5a60fd7b14e9a3045513a4b46ebd109c422c5b2e)
.pick_status.json
src/util/hash_table.c