Skip to contentSkip to conversation

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

PATCH New Utkal Singh 1 participant 1 message Apr 1 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 erofs-utils: fix pthread resource leak in erofs_alloc_workqueue()

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")
---
 lib/workqueue.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
lib/workqueue.c +5 -1
index 4a1a957..1f3fa7c 100644
@@ -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);
Signature · 1 line
2.43.0