Skip to contentSkip to conversation

btrfs : fix memory leak in add_block_entry

PATCH In review [email protected] 2 participants 2 messages Jul 7, 2020 – Jul 14, 2020 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 btrfs : fix memory leak in add_block_entry

From: Tom Rix <[email protected]>

clang static analysis flags this error

fs/btrfs/ref-verify.c:290:3: warning: Potential leak of memory pointed to by 're' [unix.Malloc]

                kfree(be);
                ^~~~~

The problem is in this block of code

		if (root_objectid) {
			struct root_entry *exist_re;

			exist_re = insert_root_entry(&exist->roots, re);
			if (exist_re)
				kfree(re);
		}

There is no 'else' block freeing when root_objectid == 0

So add an 'else'

Fixes fd708b81d972 ("Btrfs: add a extent ref verify tool")
---
 fs/btrfs/ref-verify.c | 2 ++
 1 file changed, 2 insertions(+)
fs/btrfs/ref-verify.c +2 -0
index af92525dbb16..7f03dbe5b609 100644
@@ -286,6 +286,8 @@ static struct block_entry *add_block_entry(struct btrfs_fs_info *fs_info,
 			exist_re = insert_root_entry(&exist->roots, re);
 			if (exist_re)
 				kfree(re);
+		} else {
+			kfree(re);
 		}
 		kfree(be);
 		return exist;
Signature · 1 line
2.18.1
#1 lore raw
Quoted 17 lines · On Tue, Jul 07, 2020 at 06:29:08AM -0700, [email protected] wrote:

From: Tom Rix <[email protected]>

clang static analysis flags this error

fs/btrfs/ref-verify.c:290:3: warning: Potential leak of memory pointed to by 're' [unix.Malloc]

                kfree(be);
                ^~~~~

The problem is in this block of code

		if (root_objectid) {
			struct root_entry *exist_re;

			exist_re = insert_root_entry(&exist->roots, re);
			if (exist_re)
				kfree(re);
		}
Quoted 7 lines

There is no 'else' block freeing when root_objectid == 0

So add an 'else'

Fixes fd708b81d972 ("Btrfs: add a extent ref verify tool")

Added to misc-next, thanks.