Skip to contentSkip to conversation

btrfs-progs: avoid memory leak in btrfs_close_devices

PATCH RESEND New Wang Sheng-Hui 1 participant 1 message Jun 25, 2013 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH RESEND 1/1 btrfs-progs: avoid memory leak in btrfs_close_devices

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
* fs_devices structure itself

The same ones for seed_devices.

---
 volumes.c |   16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
volumes.c +13 -3
index d6f81f8..257b740 100644
@@ -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;
 }
Signature · 1 line
1.7.10.4