linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] btrfs-progs: avoid memory leak in btrfs_close_devices
@ 2013-06-25 13:02 Wang Sheng-Hui
  2013-07-02 16:39 ` David Sterba
  2013-07-03  5:01 ` Anand Jain
  0 siblings, 2 replies; 4+ messages in thread
From: Wang Sheng-Hui @ 2013-06-25 13:02 UTC (permalink / raw)
  To: Josef Bacik, chris.mason, linux-btrfs

Three kind of structures need to be freed on close:
      * All struct btrfs_device managed by fs_devices
      * The name field for each struct btrfs_device
      * The above items for seed_devices

Signed-off-by: Wang Sheng-Hui <shhuiw@gmail.com>
---
  volumes.c |   16 +++++++++++++---
  1 file changed, 13 insertions(+), 3 deletions(-)

diff --git a/volumes.c b/volumes.c
index d6f81f8..257b740 100644
--- a/volumes.c
+++ b/volumes.c
@@ -153,6 +153,16 @@ static int device_list_add(const char *path,
  	return 0;
  }

+static void btrfs_close_device(struct btrfs_device *device)
+{
+	close(device->fd);
+	device->fd = -1;
+	device->writeable = 0;
+	if (device->name)
+		kfree(device->name);
+	kfree(device);
+}
+
  int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
  {
  	struct btrfs_fs_devices *seed_devices;
@@ -161,17 +171,17 @@ int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
  again:
  	list_for_each(cur, &fs_devices->devices) {
  		device = list_entry(cur, struct btrfs_device, dev_list);
-		close(device->fd);
-		device->fd = -1;
-		device->writeable = 0;
+		btrfs_close_device(device);
  	}

  	seed_devices = fs_devices->seed;
  	fs_devices->seed = NULL;
  	if (seed_devices) {
+		kfree(fs_devices);
  		fs_devices = seed_devices;
  		goto again;
  	}
+	kfree(fs_devices);

  	return 0;
  }
-- 
1.7.10.4


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2013-07-03  5:43 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-06-25 13:02 [PATCH] btrfs-progs: avoid memory leak in btrfs_close_devices Wang Sheng-Hui
2013-07-02 16:39 ` David Sterba
2013-07-03  5:01 ` Anand Jain
2013-07-03  5:48   ` Anand Jain

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).