[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-changelog] [xen-4.2-testing] Fix libxenstore memory leak when USE_PTHREAD is not defined


  • To: xen-changelog@xxxxxxxxxxxxxxxxxxx
  • From: Xen patchbot-4.2-testing <patchbot@xxxxxxx>
  • Date: Tue, 02 Oct 2012 16:22:07 +0000
  • Delivery-date: Tue, 02 Oct 2012 16:22:16 +0000
  • List-id: "Change log for Mercurial \(receive only\)" <xen-changelog.lists.xen.org>

# HG changeset patch
# User Andres Lagar-Cavilla <andres@xxxxxxxxxxxxxxxx>
# Date 1349169613 -3600
# Node ID 906b3a2fcd0a82f37dcdf7ac83d71c637006c6f5
# Parent  2b59edeb0fddf738d055815ab10c2d98ef724be1
Fix libxenstore memory leak when USE_PTHREAD is not defined

Redefine usage of pthread_cleanup_push and _pop, to explicitly call free for
heap objects in error paths.

By the way, set a suitable errno value for an error path that had none.

Signed-off-by: Andres Lagar-Cavilla <andres@xxxxxxxxxxxxxxxx>
Acked-by: Ian Campbell <ian.campbell@xxxxxxxxxx>
Committed-by: Ian Campbell <ian.campbell@xxxxxxxxxx>

xen-unstable changeset: 25914:cd2a831d0a41
Backport-requested-by: Andres Lagar-Cavilla <andreslc@xxxxxxxxxxxxxx>
Committed-by: Ian Jackson <ian.jackson@xxxxxxxxxxxxx>
---


diff -r 2b59edeb0fdd -r 906b3a2fcd0a tools/xenstore/xs.c
--- a/tools/xenstore/xs.c       Tue Sep 25 12:19:33 2012 +0200
+++ b/tools/xenstore/xs.c       Tue Oct 02 10:20:13 2012 +0100
@@ -110,6 +110,11 @@ struct xs_handle {
 
 #define read_thread_exists(h)  (h->read_thr_exists)
 
+/* Because pthread_cleanup_p* are not available when USE_PTHREAD is
+ * disabled, use these macros which convert appropriately. */
+#define cleanup_push_heap(p)        cleanup_push(free, p)
+#define cleanup_pop_heap(run, p)    cleanup_pop((run))
+
 static void *read_thread(void *arg);
 
 #else /* !defined(USE_PTHREAD) */
@@ -130,6 +135,9 @@ struct xs_handle {
 #define cleanup_pop(run)       ((void)0)
 #define read_thread_exists(h)  (0)
 
+#define cleanup_push_heap(p)        ((void)0)
+#define cleanup_pop_heap(run, p)    do { if ((run)) free(p); } while(0)
+
 #endif
 
 static int read_message(struct xs_handle *h, int nonblocking);
@@ -1059,7 +1067,7 @@ static int read_message(struct xs_handle
        msg = malloc(sizeof(*msg));
        if (msg == NULL)
                goto error;
-       cleanup_push(free, msg);
+       cleanup_push_heap(msg);
        if (!read_all(h->fd, &msg->hdr, sizeof(msg->hdr), nonblocking)) { /* 
Cancellation point */
                saved_errno = errno;
                goto error_freemsg;
@@ -1069,7 +1077,7 @@ static int read_message(struct xs_handle
        body = msg->body = malloc(msg->hdr.len + 1);
        if (body == NULL)
                goto error_freemsg;
-       cleanup_push(free, body);
+       cleanup_push_heap(body);
        if (!read_all(h->fd, body, msg->hdr.len, 0)) { /* Cancellation point */
                saved_errno = errno;
                goto error_freebody;
@@ -1098,6 +1106,7 @@ static int read_message(struct xs_handle
                /* There should only ever be one response pending! */
                if (!list_empty(&h->reply_list)) {
                        mutex_unlock(&h->reply_mutex);
+                       saved_errno = EEXIST;
                        goto error_freebody;
                }
 
@@ -1110,9 +1119,9 @@ static int read_message(struct xs_handle
        ret = 0;
 
 error_freebody:
-       cleanup_pop(ret == -1);
+       cleanup_pop_heap(ret == -1, body);
 error_freemsg:
-       cleanup_pop(ret == -1);
+       cleanup_pop_heap(ret == -1, msg);
 error:
        errno = saved_errno;
 

_______________________________________________
Xen-changelog mailing list
Xen-changelog@xxxxxxxxxxxxx
http://lists.xensource.com/xen-changelog


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.