]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/commitdiff
xfs_fsr: fix leaks & catch error in fsrfile()
authorEric Sandeen <sandeen@redhat.com>
Mon, 15 Sep 2014 23:18:41 +0000 (09:18 +1000)
committerDave Chinner <david@fromorbit.com>
Mon, 15 Sep 2014 23:18:41 +0000 (09:18 +1000)
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>
Reviewed-by: Brian Foster <bfoster@redhat.com>
Signed-off-by: Dave Chinner <david@fromorbit.com>
fsr/xfs_fsr.c

index 48629fde101e35e48ed385037aca1b650bdfc775..752d2dbddb3bda6eccb0fd82b82a7a7482b57ad4 100644 (file)
@@ -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);
 
-       close(fd);
+out:
+       if (fsfd >= 0)
+               close(fsfd);
+       if (fd >= 0)
+               close(fd);
+       free(fshandlep);
 
        return error;
 }