xfsprogs: fix resource leaks
cover xfsprogs: fix resource leaks
This is the result of getting bored while on "vacation: ;)
It knocks out all coverity-reported resource leaks, eliminating 16 reported defects.
Thanks,
-Eric
_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
PATCH 1/6 xfs_io: free fshandlep in parent_check()
The allocated fshandle wasn't freed in either normal exit or error paths.
Do this, and consolidate cleanup into an out: target.
--- io/parent.c | 7 ++++--- 1 files changed, 4 insertions(+), 3 deletions(-)
io/parent.c +4 -3
@@ -258,9 +258,8 @@ parent_check(void)if (!bstatbuf || !parentbuf) {fprintf (stderr,_ ("unable to allocate buffers: %s\n" ),strerror (errno)); -free (bstatbuf); -free (parentbuf); -return 1 ; + err_status =1 ; + goto out; }if (do_bulkstat (parentbuf, &parentbuf_size, bstatbuf, fsfd, fshandlep) !=0 )
@@ -272,8 +271,10 @@ parent_check(void)printf (_ ("succeeded checking %llu inodes\n" ), (unsigned long long ) inodes_checked); +out:free (bstatbuf);free (parentbuf); +free (fshandlep);return err_status; }
Signature · 6 lines
1.7.1 _______________________________________________ xfs mailing list [email protected] http://oss.sgi.com/mailman/listinfo/xfs
Re: [PATCH 1/6] xfs_io: free fshandlep in parent_check()
Quoted 7 lines · Eric Sandeen
The allocated fshandle wasn't freed in either normal exit or error paths.
Do this, and consolidate cleanup into an out: target.
Signed-off-by Eric Sandeen <[email protected]>---
Quoted 37 lines
io/parent.c | 7 ++++--- 1 files changed, 4 insertions(+), 3 deletions(-)io/parent.c +4
-3@@ -258,9 +258,8 @@ parent_check(void)if (!bstatbuf || !parentbuf) {fprintf (stderr,_ ("unable to allocate buffers: %s\n" ),strerror (errno)); -free (bstatbuf); -free (parentbuf); -return 1 ; + err_status =1 ; + goto out; }if (do_bulkstat (parentbuf, &parentbuf_size, bstatbuf, fsfd, fshandlep) !=0 )@@ -272,8 +271,10 @@ parent_check(void)printf (_ ("succeeded checking %llu inodes\n" ), (unsigned long long ) inodes_checked); +out:free (bstatbuf);free (parentbuf); +free (fshandlep);return err_status; }Signature · 6 lines
1.7.1 _______________________________________________ xfs mailing list [email protected] http://oss.sgi.com/mailman/listinfo/xfs
_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
PATCH 2/6 xfs_fsr: fix leaks & catch error in fsrfile()
In reply to Eric SandeenThe 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.
--- fsr/xfs_fsr.c | 28 ++++++++++++++-------------- 1 files changed, 14 insertions(+), 14 deletions(-)
fsr/xfs_fsr.c +14 -14
@@ -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; }
Signature · 6 lines
1.7.1 _______________________________________________ xfs mailing list [email protected] http://oss.sgi.com/mailman/listinfo/xfs
Re: [PATCH 2/6] xfs_fsr: fix leaks & catch error in fsrfile()
Quoted 71 lines · Eric Sandeen
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 byfsrfile_common. So ifgettmpname()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 <[email protected]>--- fsr/xfs_fsr.c | 28 ++++++++++++++-------------- 1 files changed, 14 insertions(+), 14 deletions(-)fsr/xfs_fsr.c +7
-11@@ -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 ;
fsr/xfs_fsr.c fsrfile(char *fname, xfs_ino_t ino) · line 857
Quoted 10 lines
fsr/xfs_fsr.c +1
-2+ 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. :-|
Quoted 17 lines
fsr/xfs_fsr.c +6
-1-close (fd); +out: +if (fsfd >=0 ) +close (fsfd); +if (fd >=0 ) +close (fd); +free (fshandlep);return error; }Signature · 6 lines
1.7.1 _______________________________________________ xfs mailing list [email protected] http://oss.sgi.com/mailman/listinfo/xfs
_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
PATCH 3/6 xfs_fsr: free handlep in fsrfs
In reply to Eric SandeenWe leaked the fshandlep in both error returns and
normal function exit.
--- fsr/xfs_fsr.c | 3 +++ 1 files changed, 3 insertions(+), 0 deletions(-)
fsr/xfs_fsr.c +3 -0
@@ -702,6 +702,7 @@ fsrfs(char *mntdir, xfs_ino_t startino, int targetrange)if ((fsfd =open (mntdir,O_RDONLY )) <0 ) {fsrprintf (_ ("unable to open: %s: %s\n" ), mntdir,strerror ( errno )); +free (fshandlep);return -1 ; }
@@ -709,6 +710,7 @@ fsrfs(char *mntdir, xfs_ino_t startino, int targetrange)fsrprintf (_ ("Skipping %s: could not get XFS geometry\n" ), mntdir);close (fsfd); +free (fshandlep);return -1 ; }
@@ -774,6 +776,7 @@ fsrfs(char *mntdir, xfs_ino_t startino, int targetrange) out0:tmp_close (mntdir);close (fsfd); +free (fshandlep);return 0 ; }
Signature · 6 lines
1.7.1 _______________________________________________ xfs mailing list [email protected] http://oss.sgi.com/mailman/listinfo/xfs
Re: [PATCH 3/6] xfs_fsr: free handlep in fsrfs
Quoted 5 lines · Eric Sandeen
We leaked the fshandlep in both error returns and
normal function exit.Signed-off-by Eric Sandeen <[email protected]>---
Same thing in the 'if (endtime && endtime < time(0))' thing towards the end of the for loop, but that ends with an exit(), so:
Quoted 38 lines
fsr/xfs_fsr.c | 3 +++ 1 files changed, 3 insertions(+), 0 deletions(-)fsr/xfs_fsr.c +3
-0@@ -702,6 +702,7 @@ fsrfs(char *mntdir, xfs_ino_t startino, int targetrange)if ((fsfd =open (mntdir,O_RDONLY )) <0 ) {fsrprintf (_ ("unable to open: %s: %s\n" ), mntdir,strerror ( errno )); +free (fshandlep);return -1 ; }@@ -709,6 +710,7 @@ fsrfs(char *mntdir, xfs_ino_t startino, int targetrange)fsrprintf (_ ("Skipping %s: could not get XFS geometry\n" ), mntdir);close (fsfd); +free (fshandlep);return -1 ; }@@ -774,6 +776,7 @@ fsrfs(char *mntdir, xfs_ino_t startino, int targetrange) out0:tmp_close (mntdir);close (fsfd); +free (fshandlep);return 0 ; }Signature · 6 lines
1.7.1 _______________________________________________ xfs mailing list [email protected] http://oss.sgi.com/mailman/listinfo/xfs
_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
PATCH 4/6 libhandle: Fix handle leak in path_to_fshandle error paths
In reply to Eric Sandeenpath_to_fshandle calls obj_to_handle, which potentially allocates a handle, but the handle isn't freed on a subsequent error path.
--- libhandle/handle.c | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
libhandle/handle.c +1 -0
@@ -97,6 +97,7 @@ path_to_fshandle(/* new filesystem. add it to the cache */ fdhp =malloc (sizeof (struct fdhash ));if (fdhp ==NULL ) { +free (*fshanp);close (fd); errno =ENOMEM ;return -1 ;
Signature · 6 lines
1.7.1 _______________________________________________ xfs mailing list [email protected] http://oss.sgi.com/mailman/listinfo/xfs
Re: [PATCH 4/6] libhandle: Fix handle leak in path_to_fshandle error paths
Quoted 14 lines · Eric Sandeen
path_to_fshandlecallsobj_to_handle, which potentially allocates a handle, but the handle isn't freed on a subsequent error path.Signed-off-by Eric Sandeen <[email protected]>--- libhandle/handle.c | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)libhandle/handle.c +0
-0
libhandle/handle.c path_to_fshandle( · line 100
Quoted 4 lines
libhandle/handle.c +1
-0@@ -97,6 +97,7 @@ path_to_fshandle(/* new filesystem. add it to the cache */ fdhp =malloc (sizeof (struct fdhash ));if (fdhp ==NULL ) { +free (*fshanp);
Might be a good idea to set *fshanp back to NULL here. It doesn't appear to be an issue with current callers, however:
Quoted 10 lines
libhandle/handle.c +0
-0close (fd); errno =ENOMEM ;return -1 ;Signature · 6 lines
1.7.1 _______________________________________________ xfs mailing list [email protected] http://oss.sgi.com/mailman/listinfo/xfs
_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
PATCH 5/6 xfs_io: fix leaks in parent_list()
In reply to Eric Sandeenparent_list() has instances where a handle is leaked, both by going out of scope, and on error paths.
--- io/parent.c | 6 ++++-- 1 files changed, 4 insertions(+), 2 deletions(-)
io/parent.c +4 -2
@@ -293,7 +293,7 @@ print_parent_entry(parent_t *parent, int fullpath)static int parent_list (int fullpath) { -void *handlep; +void *handlep =NULL ;size_t handlen;int error, i;int retval =1 ;
@@ -313,6 +313,7 @@ parent_list(int fullpath) progname, path, strerror(errno)); goto error; } +free_handle (fshandle, fshlen); }if (path_to_handle (path, &handlep, &handlen) !=0 ) {
@@ -325,7 +326,7 @@ parent_list(int fullpath)if (!parentbuf) {fprintf (stderr,_ ("%s: unable to allocate parent buffer: %s\n" ), progname,strerror (errno)); -return 1 ; + goto error; }if (fullpath) {
@@ -365,6 +366,7 @@ parent_list(int fullpath) retval =0 ; error: +free (handlep);free (parentbuf);return retval; }
Signature · 6 lines
1.7.1 _______________________________________________ xfs mailing list [email protected] http://oss.sgi.com/mailman/listinfo/xfs
Re: [PATCH 5/6] xfs_io: fix leaks in parent_list()
Quoted 5 lines · Eric Sandeen
parent_list()has instances where a handle is leaked, both by going out of scope, and on error paths.Signed-off-by Eric Sandeen <[email protected]>---
Quoted 48 lines
io/parent.c | 6 ++++-- 1 files changed, 4 insertions(+), 2 deletions(-)io/parent.c +4
-2@@ -293,7 +293,7 @@ print_parent_entry(parent_t *parent, int fullpath)static int parent_list (int fullpath) { -void *handlep; +void *handlep =NULL ;size_t handlen;int error, i;int retval =1 ;@@ -313,6 +313,7 @@ parent_list(int fullpath) progname, path, strerror(errno)); goto error; } +free_handle (fshandle, fshlen); }if (path_to_handle (path, &handlep, &handlen) !=0 ) {@@ -325,7 +326,7 @@ parent_list(int fullpath)if (!parentbuf) {fprintf (stderr,_ ("%s: unable to allocate parent buffer: %s\n" ), progname,strerror (errno)); -return 1 ; + goto error; }if (fullpath) {@@ -365,6 +366,7 @@ parent_list(int fullpath) retval =0 ; error: +free (handlep);free (parentbuf);return retval; }Signature · 6 lines
1.7.1 _______________________________________________ xfs mailing list [email protected] http://oss.sgi.com/mailman/listinfo/xfs
_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
PATCH 6/6 xfs_db: free flist on error in write_struct()
In reply to Eric SandeenOne error path in write_struct() wasn't freeing
the flist_t *fl which was allocated, so it leaks.
--- db/write.c | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
db/write.c +1 -0
@@ -652,6 +652,7 @@ write_struct( buf =convert_arg (argv[1 ], bit_length);if (!buf) {dbprintf (_ ("unable to convert value '%s'.\n" ), argv[1 ]); +flist_free (fl);return ; }
Signature · 6 lines
1.7.1 _______________________________________________ xfs mailing list [email protected] http://oss.sgi.com/mailman/listinfo/xfs
Re: [PATCH 6/6] xfs_db: free flist on error in write_struct()
Quoted 5 lines · Eric Sandeen
One error path in
write_struct()wasn't freeing
theflist_t*fl which was allocated, so it leaks.Signed-off-by Eric Sandeen <[email protected]>---
Quoted 22 lines
db/write.c | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)db/write.c +1
-0@@ -652,6 +652,7 @@ write_struct( buf =convert_arg (argv[1 ], bit_length);if (!buf) {dbprintf (_ ("unable to convert value '%s'.\n" ), argv[1 ]); +flist_free (fl);return ; }Signature · 6 lines
1.7.1 _______________________________________________ xfs mailing list [email protected] http://oss.sgi.com/mailman/listinfo/xfs
_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs