public inbox for linux-xfs@vger.kernel.org
 help / color / mirror / Atom feed
From: Brian Foster <bfoster@redhat.com>
To: Eric Sandeen <sandeen@redhat.com>
Cc: xfs@oss.sgi.com
Subject: Re: [PATCH 2/6] xfs_fsr: fix leaks & catch error  in fsrfile()
Date: Tue, 5 Aug 2014 09:16:18 -0400	[thread overview]
Message-ID: <20140805131618.GC53538@bfoster.bfoster> (raw)
In-Reply-To: <1406905159-12415-3-git-send-email-sandeen@redhat.com>

On Fri, Aug 01, 2014 at 09:59:15AM -0500, Eric Sandeen wrote:
> The allocated fshandlep leaks on most error paths;
> restructure with an out: target that does all necessary
> freeing, and initialize filehandles to -1 so that we
> know whether they need to be closed on the error path.
> 
> While we're at it, if gettmpname() fails, we still
> return 0 for an error, because error is initialized
> to 0 and only set otherwise by fsrfile_common.
> So if gettmpname() fails, we return success from the
> function even though we did no work.  Fix that
> as well by initializing error to -1.
> 
> Signed-off-by: Eric Sandeen <sandeen@redhat.com>
> ---
>  fsr/xfs_fsr.c |   28 ++++++++++++++--------------
>  1 files changed, 14 insertions(+), 14 deletions(-)
> 
> diff --git a/fsr/xfs_fsr.c b/fsr/xfs_fsr.c
> index 48629fd..752d2db 100644
> --- a/fsr/xfs_fsr.c
> +++ b/fsr/xfs_fsr.c
> @@ -809,15 +809,15 @@ fsrfile(char *fname, xfs_ino_t ino)
>  {
>  	xfs_bstat_t	statbuf;
>  	jdm_fshandle_t	*fshandlep;
> -	int	fd, fsfd;
> -	int	error = 0;
> +	int	fd = -1, fsfd = -1;
> +	int	error = -1;
>  	char	*tname;
>  
>  	fshandlep = jdm_getfshandle(getparent (fname) );
> -	if (! fshandlep) {
> +	if (!fshandlep) {
>  		fsrprintf(_("unable to construct sys handle for %s: %s\n"),
>  			fname, strerror(errno));
> -		return -1;
> +		goto out;
>  	}
>  
>  	/*
> @@ -828,39 +828,39 @@ fsrfile(char *fname, xfs_ino_t ino)
>  	if (fsfd < 0) {
>  		fsrprintf(_("unable to open sys handle for %s: %s\n"),
>  			fname, strerror(errno));
> -		return -1;
> +		goto out;
>  	}
>  
>  	if ((xfs_bulkstat_single(fsfd, &ino, &statbuf)) < 0) {
>  		fsrprintf(_("unable to get bstat on %s: %s\n"),
>  			fname, strerror(errno));
> -		close(fsfd);
> -		return -1;
> +		goto out;
>  	}
>  
>  	fd = jdm_open(fshandlep, &statbuf, O_RDWR|O_DIRECT);
>  	if (fd < 0) {
>  		fsrprintf(_("unable to open handle %s: %s\n"),
>  			fname, strerror(errno));
> -		close(fsfd);
> -		return -1;
> +		goto out;
>  	}
>  
>  	/* Get the fs geometry */
>  	if (xfs_getgeom(fsfd, &fsgeom) < 0 ) {
>  		fsrprintf(_("Unable to get geom on fs for: %s\n"), fname);
> -		close(fsfd);
> -		return -1;
> +		goto out;
>  	}
>  
> -	close(fsfd);
> -
>  	tname = gettmpname(fname);
>  
>  	if (tname)
>  		error = fsrfile_common(fname, tname, NULL, fd, &statbuf);
>  

I was wondering whether this bit to not fail if the path is bad was
intentional (e.g., to avoid breaking through a higher-level loop or
something), but we don't check the return value of this function
anyways. :-|

Reviewed-by: Brian Foster <bfoster@redhat.com>

> -	close(fd);
> +out:
> +	if (fsfd >= 0)
> +		close(fsfd);
> +	if (fd >= 0)
> +		close(fd);
> +	free(fshandlep);
>  
>  	return error;
>  }
> -- 
> 1.7.1
> 
> _______________________________________________
> xfs mailing list
> xfs@oss.sgi.com
> http://oss.sgi.com/mailman/listinfo/xfs

_______________________________________________
xfs mailing list
xfs@oss.sgi.com
http://oss.sgi.com/mailman/listinfo/xfs

  reply	other threads:[~2014-08-05 13:16 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-08-01 14:59 [PATCH 0/6] xfsprogs: fix resource leaks Eric Sandeen
2014-08-01 14:59 ` [PATCH 1/6] xfs_io: free fshandlep in parent_check() Eric Sandeen
2014-08-05 13:16   ` Brian Foster
2014-08-01 14:59 ` [PATCH 2/6] xfs_fsr: fix leaks & catch error in fsrfile() Eric Sandeen
2014-08-05 13:16   ` Brian Foster [this message]
2014-08-01 14:59 ` [PATCH 3/6] xfs_fsr: free handlep in fsrfs Eric Sandeen
2014-08-05 13:16   ` Brian Foster
2014-08-01 14:59 ` [PATCH 4/6] libhandle: Fix handle leak in path_to_fshandle error paths Eric Sandeen
2014-08-05 13:31   ` Brian Foster
2014-08-01 14:59 ` [PATCH 5/6] xfs_io: fix leaks in parent_list() Eric Sandeen
2014-08-05 13:31   ` Brian Foster
2014-08-01 14:59 ` [PATCH 6/6] xfs_db: free flist on error in write_struct() Eric Sandeen
2014-08-05 13:31   ` Brian Foster

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=20140805131618.GC53538@bfoster.bfoster \
    --to=bfoster@redhat.com \
    --cc=sandeen@redhat.com \
    --cc=xfs@oss.sgi.com \
    /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