Module: Mesa
Branch: main
Commit: 11ab6087797f805cf158048915c67945613c9a72
URL:    
http://cgit.freedesktop.org/mesa/mesa/commit/?id=11ab6087797f805cf158048915c67945613c9a72

Author: Yonggang Luo <[email protected]>
Date:   Sun Aug  7 23:46:13 2022 +0800

util: Fixes memory leak in create_cache_item_header_and_blob

stack-trace by asan
Direct leak of 238 byte(s) in 6 object(s) allocated from:
    #0 0x7f101e1dbe8f in __interceptor_malloc 
../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
    #1 0x55ce3a959e2c in create_cache_item_header_and_blob 
../src/util/disk_cache_os.c:647
    #2 0x55ce3a95c217 in disk_cache_write_item_to_disk_foz 
../src/util/disk_cache_os.c:967
    #3 0x55ce3a97bce1 in util_queue_thread_func ../src/util/u_queue.c:306
    #4 0x55ce3a989c3b in impl_thrd_routine ../src/c11/impl/threads_posix.c:67
    #5 0x7f101ddc7ea6 in start_thread nptl/pthread_create.c:477

Fixes: 2a9b4ad1774 ("util/disk_cache: Add option to disable compression")

Cc: mesa-stable

Signed-off-by: Yonggang Luo <[email protected]>
Reviewed-by: Eric Engestrom <[email protected]>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/18024>

---

 src/util/disk_cache_os.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/src/util/disk_cache_os.c b/src/util/disk_cache_os.c
index da31959533c..e080fec9d65 100644
--- a/src/util/disk_cache_os.c
+++ b/src/util/disk_cache_os.c
@@ -644,14 +644,15 @@ create_cache_item_header_and_blob(struct 
disk_cache_put_job *dc_job,
    /* Compress the cache item data */
    size_t max_buf = util_compress_max_compressed_len(dc_job->size);
    size_t compressed_size;
-   void *compressed_data = malloc(max_buf);
-   if (compressed_data == NULL)
-      return false;
+   void *compressed_data;
 
    if (dc_job->cache->compression_disabled) {
       compressed_size = dc_job->size;
       compressed_data = dc_job->data;
    } else {
+      compressed_data = malloc(max_buf);
+      if (compressed_data == NULL)
+         return false;
       compressed_size =
          util_compress_deflate(dc_job->data, dc_job->size,
                               compressed_data, max_buf);

Reply via email to