Skip to contentSkip to conversation

btrfs-progs: avoid memory leak in find_and_setup_log_root

PATCH New Wang Sheng-Hui 1 participant 1 message Oct 25, 2012 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 btrfs-progs: avoid memory leak in find_and_setup_log_root

In find_and_setup_log_root, the malloced log_root would be leaked if we have bytenr = 0, which would happen at our mkfs stage.

Move the memory allocation after the bytenr check, and add allocation failure check.

---
 disk-io.c |    6 +++++-
 1 files changed, 5 insertions(+), 1 deletions(-)
disk-io.c +5 -1
index 0395205..3a80284 100644
@@ -456,11 +456,15 @@ static int find_and_setup_log_root(struct btrfs_root *tree_root,
 {
 	u32 blocksize;
 	u64 blocknr = btrfs_super_log_root(disk_super);
-	struct btrfs_root *log_root = malloc(sizeof(struct btrfs_root));
+	struct btrfs_root *log_root = NULL;
 
 	if (blocknr == 0)
 		return 0;
 
+	log_root = malloc(sizeof(struct btrfs_root));
+	if (!log_root)
+		return -ENOMEM;
+
 	blocksize = btrfs_level_size(tree_root,
 			     btrfs_super_log_root_level(disk_super));
 
Signature · 1 line
1.7.5.4