util: Fixes memory leak in create_cache_item_header_and_blob
authorYonggang Luo <luoyonggang@gmail.com>
Sun, 7 Aug 2022 15:46:13 +0000 (23:46 +0800)
committerMarge Bot <emma+marge@anholt.net>
Fri, 12 Aug 2022 18:06:36 +0000 (18:06 +0000)
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 <luoyonggang@gmail.com>
Reviewed-by: Eric Engestrom <eric@igalia.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/18024>

src/util/disk_cache_os.c

index da31959..e080fec 100644 (file)
@@ -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);