[PATCH] erofs-utils: fix pthread resource leak in erofs_alloc_workqueue()

Utkal Singh <[email protected]>
Newsgroups org.ozlabs.lists.linux-erofs
Message-ID <[email protected]>
When malloc() fails to allocate wq->workers, erofs_alloc_workqueue()
returns -ENOMEM without cleaning up previously initialized mutex and
condition variables. Destroy them before returning to avoid a resource leak.

Fixes: 13f7268 ("erofs-utils: introduce multi-threading framework")

Signed-off-by: Utkal Singh <[email protected]>
---
 lib/workqueue.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/lib/workqueue.c b/lib/workqueue.c
index 4a1a957..1f3fa7c 100644
--- a/lib/workqueue.c
+++ b/lib/workqueue.c
@@ -90,8 +90,12 @@ int erofs_alloc_workqueue(struct erofs_workqueue *wq, unsigned int nworker,
 	pthread_cond_init(&wq->cond_full, NULL);
 
 	wq->workers = malloc(nworker * sizeof(pthread_t));
-	if (!wq->workers)
+	if (!wq->workers) {
+		pthread_mutex_destroy(&wq->lock);
+		pthread_cond_destroy(&wq->cond_empty);
+		pthread_cond_destroy(&wq->cond_full);
 		return -ENOMEM;
+	}
 
 	for (i = 0; i < nworker; i++) {
 		ret = -pthread_create(&wq->workers[i], NULL, worker_thread, wq);
-- 
2.43.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.