Mesa (main): util/queue: Don't crash in util_queue_destroy when init failed

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Wed Jul 28 18:00:25 UTC 2021


Module: Mesa
Branch: main
Commit: d78e980523b4099aeff38e398d7fc795264d8859
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=d78e980523b4099aeff38e398d7fc795264d8859

Author: Ian Romanick <ian.d.romanick at intel.com>
Date:   Mon Jun  7 12:04:03 2021 -0700

util/queue: Don't crash in util_queue_destroy when init failed

This simplifies the error exit paths for drivers that use these queues.

v2: Move allocation of queue->jobs after initializing the mutxes and
condition variables.  Noticed by Ken.

Reviewed-by: Kenneth Graunke <kenneth at whitecape.org>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/11229>

---

 src/util/u_queue.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)

diff --git a/src/util/u_queue.c b/src/util/u_queue.c
index deb6a795c53..7356ffbd670 100644
--- a/src/util/u_queue.c
+++ b/src/util/u_queue.c
@@ -445,11 +445,6 @@ util_queue_init(struct util_queue *queue,
    queue->max_jobs = max_jobs;
    queue->global_data = global_data;
 
-   queue->jobs = (struct util_queue_job*)
-                 calloc(max_jobs, sizeof(struct util_queue_job));
-   if (!queue->jobs)
-      goto fail;
-
    (void) mtx_init(&queue->lock, mtx_plain);
    (void) mtx_init(&queue->finish_lock, mtx_plain);
 
@@ -457,6 +452,11 @@ util_queue_init(struct util_queue *queue,
    cnd_init(&queue->has_queued_cond);
    cnd_init(&queue->has_space_cond);
 
+   queue->jobs = (struct util_queue_job*)
+                 calloc(max_jobs, sizeof(struct util_queue_job));
+   if (!queue->jobs)
+      goto fail;
+
    queue->threads = (thrd_t*) calloc(queue->max_threads, sizeof(thrd_t));
    if (!queue->threads)
       goto fail;
@@ -534,7 +534,10 @@ void
 util_queue_destroy(struct util_queue *queue)
 {
    util_queue_kill_threads(queue, 0, false);
-   remove_from_atexit_list(queue);
+
+   /* This makes it safe to call on a queue that failedutil_queue_init. */
+   if (queue->head.next != NULL)
+      remove_from_atexit_list(queue);
 
    cnd_destroy(&queue->has_space_cond);
    cnd_destroy(&queue->has_queued_cond);



More information about the mesa-commit mailing list