]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
daemon, threads: plug a memory leak
authorEric Blake <eblake@redhat.com>
Sat, 11 Dec 2010 00:35:58 +0000 (17:35 -0700)
committerEric Blake <eblake@redhat.com>
Tue, 14 Dec 2010 00:01:14 +0000 (17:01 -0700)
* daemon/libvirtd.c (qemudStartWorker, qemudStartEventLoop): Avoid
leaking pthread_attr resources.
* src/util/threads-pthread.c (virThreadCreate): Likewise.

daemon/libvirtd.c
src/util/threads-pthread.c

index 2914f2fa8a6415850cf935a2c104165bbfdffc10..14777dd94adfe9a3bdeeedafa816e83bf904071d 100644 (file)
@@ -1600,15 +1600,20 @@ static void *qemudWorker(void *data)
     }
 }
 
-static int qemudStartWorker(struct qemud_server *server,
-                            struct qemud_worker *worker) {
+static int
+qemudStartWorker(struct qemud_server *server,
+                 struct qemud_worker *worker)
+{
     pthread_attr_t attr;
-    pthread_attr_init(&attr);
+    int ret = -1;
+
+    if (pthread_attr_init(&attr) != 0)
+        return -1;
     /* We want to join workers, so don't detach them */
     /*pthread_attr_setdetachstate(&attr, 1);*/
 
     if (worker->hasThread)
-        return -1;
+        goto cleanup;
 
     worker->server = server;
     worker->hasThread = 1;
@@ -1621,10 +1626,13 @@ static int qemudStartWorker(struct qemud_server *server,
                        worker) != 0) {
         worker->hasThread = 0;
         worker->server = NULL;
-        return -1;
+        goto cleanup;
     }
 
-    return 0;
+    ret = 0;
+cleanup:
+    pthread_attr_destroy(&attr);
+    return ret;
 }
 
 
@@ -2414,9 +2422,14 @@ cleanup:
 }
 
 
-static int qemudStartEventLoop(struct qemud_server *server) {
+static int
+qemudStartEventLoop(struct qemud_server *server)
+{
     pthread_attr_t attr;
-    pthread_attr_init(&attr);
+    int ret = -1;
+
+    if (pthread_attr_init(&attr) != 0)
+        return -1;
     /* We want to join the eventloop, so don't detach it */
     /*pthread_attr_setdetachstate(&attr, 1);*/
 
@@ -2424,11 +2437,14 @@ static int qemudStartEventLoop(struct qemud_server *server) {
                        &attr,
                        qemudRunLoop,
                        server) != 0)
-        return -1;
+        goto cleanup;
 
     server->hasEventThread = 1;
 
-    return 0;
+    ret = 0;
+cleanup:
+    pthread_attr_destroy(&attr);
+    return ret;
 }
 
 
index c406f27de084f3d012c8f0047314cd0a98f5051d..ea71589bc4c9b6c9479ecee0ad4f89fce3352c38 100644 (file)
@@ -157,9 +157,15 @@ int virThreadCreate(virThreadPtr thread,
 {
     struct virThreadArgs *args;
     pthread_attr_t attr;
-    pthread_attr_init(&attr);
-    if (VIR_ALLOC(args) < 0)
-        return -1;
+    int ret = -1;
+    int err;
+
+    if ((err = pthread_attr_init(&attr)) != 0)
+        goto cleanup;
+    if (VIR_ALLOC(args) < 0) {
+        err = ENOMEM;
+        goto cleanup;
+    }
 
     args->func = func;
     args->opaque = opaque;
@@ -167,14 +173,19 @@ int virThreadCreate(virThreadPtr thread,
     if (!joinable)
         pthread_attr_setdetachstate(&attr, 1);
 
-    int ret = pthread_create(&thread->thread, &attr, virThreadHelper, args);
-    if (ret != 0) {
+    err = pthread_create(&thread->thread, &attr, virThreadHelper, args);
+    if (err != 0) {
         VIR_FREE(args);
-        errno = ret;
-        return -1;
+        goto cleanup;
     }
     /* New thread owns 'args' in success case, so don't free */
-    return 0;
+
+    ret = 0;
+cleanup:
+    pthread_attr_destroy(&attr);
+    if (ret < 0)
+        errno = err;
+    return ret;
 }
 
 void virThreadSelf(virThreadPtr thread)