Skip to content

Commit

Permalink
remove mem leak in error cases, for example on error by write or if i…
Browse files Browse the repository at this point in the history
…t was cut or by partial extraction (no return without free, done list may contain not processed entries, so library leaks);

+ small code deduplication
  • Loading branch information
sebres committed Sep 27, 2023
1 parent ce27b4a commit 396505c
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions C/zstdmt/brotli-mt_decompress.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 */
Expand All @@ -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;
Expand Down

0 comments on commit 396505c

Please sign in to comment.