util/mesa-db: Fix crash on compacting empty DB

Fix mesa_db_compact() segfaulting if compacted DB is empty. This crash
happens on writing cache entry that is bigger than DB's size limit and
when DB is empty, which can be triggered by setting DB size to a small
value.

Fixes: 32211788d0 ("util/disk_cache: Add new mesa-db cache type")
Signed-off-by: Dmitry Osipenko <dmitry.osipenko@collabora.com>
Acked-by: Timothy Arceri <tarceri@itsqueeze.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/30988>
(cherry picked from commit 5ec424c6be)
This commit is contained in:
Dmitry Osipenko 2024-10-15 22:20:55 +03:00 committed by Eric Engestrom
parent 1421945edd
commit ffbb9de588
2 changed files with 4 additions and 1 deletions

View file

@ -524,7 +524,7 @@
"description": "util/mesa-db: Fix crash on compacting empty DB",
"nominated": true,
"nomination_type": 1,
"resolution": 0,
"resolution": 1,
"main_sha": null,
"because_sha": "32211788d05311d1a92863e5445b56a5175ad4a0",
"notes": null

View file

@ -524,6 +524,9 @@ mesa_db_compact(struct mesa_cache_db *db, int64_t blob_size,
return false;
num_entries = _mesa_hash_table_num_entries(db->index_db->table);
if (!num_entries)
return true;
entries = calloc(num_entries, sizeof(*entries));
if (!entries)
return false;