From 396505c7d702cdc2fda8825cb5478f05f8dae6e0 Mon Sep 17 00:00:00 2001 From: sebres Date: Tue, 26 Sep 2023 17:43:30 +0200 Subject: [PATCH 1/2] remove mem leak in error cases, for example on error by write or if it was cut or by partial extraction (no return without free, done list may contain not processed entries, so library leaks); + small code deduplication --- C/zstdmt/brotli-mt_decompress.c | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/C/zstdmt/brotli-mt_decompress.c b/C/zstdmt/brotli-mt_decompress.c index 6538cd14..071956cb 100644 --- a/C/zstdmt/brotli-mt_decompress.c +++ b/C/zstdmt/brotli-mt_decompress.c @@ -363,12 +363,7 @@ static void *pt_decompress(void *arg) } /* everything is okay */ - pthread_mutex_lock(&ctx->write_mutex); - list_move(&wl->node, &ctx->writelist_free); - pthread_mutex_unlock(&ctx->write_mutex); - if (in->allocated) - free(in->buf); - return 0; + result = 0; error_lock: pthread_mutex_lock(&ctx->write_mutex); @@ -508,8 +503,8 @@ size_t BROTLIMT_decompressDCtx(BROTLIMT_DCtx * ctx, BROTLIMT_RdWr_t * rdwr) /* no pthread_create() needed! */ void *p = pt_decompress(w); if (p) - return (size_t) p; - goto okay; + retval_of_thread = p; + goto done; } /* multi threaded */ @@ -530,7 +525,16 @@ size_t BROTLIMT_decompressDCtx(BROTLIMT_DCtx * ctx, BROTLIMT_RdWr_t * rdwr) retval_of_thread = p; } - okay: + done: + /* move remaining done/busy entries to free list */ + while (!list_empty(&ctx->writelist_done)) { + struct list_head *entry = list_first(&ctx->writelist_done); + list_move(entry, &ctx->writelist_free); + } + while (!list_empty(&ctx->writelist_busy)) { + struct list_head* entry = list_first(&ctx->writelist_busy); + list_move(entry, &ctx->writelist_free); + } /* clean up the buffers */ while (!list_empty(&ctx->writelist_free)) { struct writelist *wl; From fa7e6314cb085fad66311769082c84d9fc2b7660 Mon Sep 17 00:00:00 2001 From: sebres Date: Wed, 27 Sep 2023 15:10:39 +0200 Subject: [PATCH 2/2] remove possible mem leak in error cases by brotli compression too --- C/zstdmt/brotli-mt_compress.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/C/zstdmt/brotli-mt_compress.c b/C/zstdmt/brotli-mt_compress.c index d8dad2b4..c0db8272 100644 --- a/C/zstdmt/brotli-mt_compress.c +++ b/C/zstdmt/brotli-mt_compress.c @@ -480,6 +480,15 @@ size_t BROTLIMT_compressCCtx(BROTLIMT_CCtx * ctx, BROTLIMT_RdWr_t * rdwr) retval_of_thread = p; } + /* move remaining done/busy entries to free list */ + while (!list_empty(&ctx->writelist_done)) { + struct list_head *entry = list_first(&ctx->writelist_done); + list_move(entry, &ctx->writelist_free); + } + while (!list_empty(&ctx->writelist_busy)) { + struct list_head* entry = list_first(&ctx->writelist_busy); + list_move(entry, &ctx->writelist_free); + } /* clean up lists */ while (!list_empty(&ctx->writelist_free)) { struct writelist *wl;