xen-devel.lists.xenproject.org archive mirror
 help / color / mirror / Atom feed
From: Ian Campbell <Ian.Campbell@citrix.com>
To: Andres Lagar-Cavilla <andres@lagarcavilla.org>
Cc: "andres@gridcentric.ca" <andres@gridcentric.ca>,
	Ian Jackson <Ian.Jackson@eu.citrix.com>,
	"xen-devel@lists.xen.org" <xen-devel@lists.xen.org>
Subject: Re: [PATCH] Fix libxenstore memory leak when USE_PTHREAD is not defined, V2
Date: Fri, 14 Sep 2012 16:10:03 +0100	[thread overview]
Message-ID: <1347635403.24226.230.camel@zakaz.uk.xensource.com> (raw)
In-Reply-To: <a83fd69dcb277ce12bf6.1347634735@xdev.gridcentric.ca>

On Fri, 2012-09-14 at 15:58 +0100, Andres Lagar-Cavilla wrote:
> tools/xenstore/xs.c |  17 +++++++++++++----
>  1 files changed, 13 insertions(+), 4 deletions(-)
> 
> 
> 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.
> 
> This is V2 after comments from Ian Campbell.
> 
> Signed-off-by: Andres Lagar-Cavilla <andres@lagarcavilla.org>

Looks good to me

Acked-by: Ian Campbell <ian.campbell@citrix.com>

> 
> diff -r 588d0dc298a4 -r a83fd69dcb27 tools/xenstore/xs.c
> --- a/tools/xenstore/xs.c
> +++ b/tools/xenstore/xs.c
> @@ -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;
>  

  reply	other threads:[~2012-09-14 15:10 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-09-14 14:58 [PATCH] Fix libxenstore memory leak when USE_PTHREAD is not defined, V2 Andres Lagar-Cavilla
2012-09-14 15:10 ` Ian Campbell [this message]
2012-09-17 10:18   ` Ian Campbell

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1347635403.24226.230.camel@zakaz.uk.xensource.com \
    --to=ian.campbell@citrix.com \
    --cc=Ian.Jackson@eu.citrix.com \
    --cc=andres@gridcentric.ca \
    --cc=andres@lagarcavilla.org \
    --cc=xen-devel@lists.xen.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).