From f3e3d31502c972e8dfbab391dda84f069e9b6b87 Mon Sep 17 00:00:00 2001 From: Theodore Ts'o Date: Thu, 21 Feb 2002 20:48:25 -0500 Subject: [PATCH] ismounted.c (is_swap_device): Fix file descriptor/memory leak; we were missing an fclose(). --- lib/ext2fs/ChangeLog | 10 ++++++++-- lib/ext2fs/ismounted.c | 19 ++++++++++++------- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/lib/ext2fs/ChangeLog b/lib/ext2fs/ChangeLog index e32db2d..1b10a07 100644 --- a/lib/ext2fs/ChangeLog +++ b/lib/ext2fs/ChangeLog @@ -1,10 +1,16 @@ +2002-02-21 Theodore Tso + + * ismounted.c (is_swap_device): Fix file descriptor/memory leak; + we were missing an fclose(). + 2002-02-20 Theodore Tso * Makefile.in, inode_io.c, ext2fs.h, ext2_err.et.in: Add new io abstraction interface which exports an ext2 inode. - * fileio.c (ext2fs_file_flush): Export ext2fs_file_flush as a - public interface. + * ext2fs.h, fileio.c (ext2fs_file_flush): Export ext2fs_file_flush + as a public interface. Change void * to const void * in + ext2fs_file_write's interface. * test_io.c (test_close), unix_io.c (unix_close): Remove unneeded conditional; save a few bytes. diff --git a/lib/ext2fs/ismounted.c b/lib/ext2fs/ismounted.c index dd853e2..858d08e 100644 --- a/lib/ext2fs/ismounted.c +++ b/lib/ext2fs/ismounted.c @@ -155,6 +155,7 @@ static int is_swap_device(const char *file) char buf[1024], *cp; dev_t file_dev; struct stat st_buf; + int ret = 0; file_dev = 0; #ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */ @@ -162,8 +163,7 @@ static int is_swap_device(const char *file) file_dev = st_buf.st_rdev; #endif - f = fopen("/proc/swaps", "r"); - if (!f) + if (!(f = fopen("/proc/swaps", "r"))) return 0; /* Skip the first line */ fgets(buf, sizeof(buf), f); @@ -174,15 +174,20 @@ static int is_swap_device(const char *file) *cp = 0; if ((cp = strchr(buf, '\t')) != NULL) *cp = 0; - if (strcmp(buf, file) == 0) - return 1; + if (strcmp(buf, file) == 0) { + ret++; + break; + } #ifndef __GNU__ if (file_dev && (stat(buf, &st_buf) == 0) && - file_dev == st_buf.st_rdev) - return 1; + file_dev == st_buf.st_rdev) { + ret++; + break; + } #endif } - return 0; + fclose(f); + return ret; } static errcode_t check_mntent(const char *file, int *mount_flags, -- 1.8.3.1