Skip to contentSkip to conversation

e2fsprogs: fix memory leak in ext2fs_free_generic_bmap()

PATCH In review Lukas Czerner 2 participants 3 messages Mar 21, 2011 – May 9, 2011

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 e2fsprogs: fix memory leak in ext2fs_free_generic_bmap()

In ext2fs_free_generic_bmap() when we are freeing 64-bit bitmap, we do call free_bmap() to free backend specific bitmap structures, however we should also free ext2fs_generic_bitmap structure as well. So this commit fixes that and removes unnecessary assignments.

---
 lib/ext2fs/gen_bitmap64.c |    8 ++++----
 1 files changed, 4 insertions(+), 4 deletions(-)
lib/ext2fs/gen_bitmap64.c +4 -4
index 8b9e4c5..f343cc7 100644
@@ -153,11 +153,11 @@ void ext2fs_free_generic_bmap(ext2fs_generic_bitmap bmap)
 
 	bmap->bitmap_ops->free_bmap(bmap);
 
-	if (bmap->description) {
+	if (bmap->description)
 		ext2fs_free_mem(&bmap->description);
-		bmap->description = 0;
-	}
-	bmap->magic = 0;
+
+	ext2fs_free_mem(&bmap);
+	bmap = NULL;
 }
 
 errcode_t ext2fs_copy_generic_bmap(ext2fs_generic_bitmap src,
Signature · 1 line
1.7.4
#1 lore raw
Quoted 17 lines · Lukas Czerner

In ext2fs_free_generic_bmap() when we are freeing 64-bit bitmap, we do call free_bmap() to free backend specific bitmap structures, however we should also free ext2fs_generic_bitmap structure as well. So this commit fixes that and removes unnecessary assignments.

---
 lib/ext2fs/gen_bitmap64.c |    8 ++++----
 1 files changed, 4 insertions(+), 4 deletions(-)
lib/ext2fs/gen_bitmap64.c +0 -0
index 8b9e4c5..f343cc7 100644
@@ -153,11 +153,11 @@ void ext2fs_free_generic_bmap(ext2fs_generic_bitmap bmap)
 
 	bmap->bitmap_ops->free_bmap(bmap);

lib/ext2fs/gen_bitmap64.c void ext2fs_free_generic_bmap(ext2fs_generic_bitmap bmap) · line 160

Quoted 10 lines
lib/ext2fs/gen_bitmap64.c +4 -4
 
-	if (bmap->description) {
+	if (bmap->description)
 		ext2fs_free_mem(&bmap->description);
-		bmap->description = 0;
-	}
-	bmap->magic = 0;
+
+	ext2fs_free_mem(&bmap);
+	bmap = NULL;

Setting bmap to NULL is pointless since C doesn't have pass-by-reference ala C++ (i.e., it's not like this function parameter is declared "ext2fs_generic_bitmap &bmap").

So it's actually a good idea to zap bmap->description, and especially bmap->magic, since that will allow us to catch attempts to use the bitmap after we free it.

I'll make the simplification of your patch which just adds the missing "ext2fs_free_mem(&bmap)" call.

- Ted

#2 lore raw
Quoted 33 lines · Ted Ts'o
Quoted 17 lines · Lukas Czerner

In ext2fs_free_generic_bmap() when we are freeing 64-bit bitmap, we do call free_bmap() to free backend specific bitmap structures, however we should also free ext2fs_generic_bitmap structure as well. So this commit fixes that and removes unnecessary assignments.

---
 lib/ext2fs/gen_bitmap64.c |    8 ++++----
 1 files changed, 4 insertions(+), 4 deletions(-)
lib/ext2fs/gen_bitmap64.c +0 -0
index 8b9e4c5..f343cc7 100644
@@ -153,11 +153,11 @@ void ext2fs_free_generic_bmap(ext2fs_generic_bitmap bmap)
 
 	bmap->bitmap_ops->free_bmap(bmap);

lib/ext2fs/gen_bitmap64.c void ext2fs_free_generic_bmap(ext2fs_generic_bitmap bmap) · line 160

Quoted 10 lines
lib/ext2fs/gen_bitmap64.c +4 -4
 
-	if (bmap->description) {
+	if (bmap->description)
 		ext2fs_free_mem(&bmap->description);
-		bmap->description = 0;
-	}
-	bmap->magic = 0;
+
+	ext2fs_free_mem(&bmap);
+	bmap = NULL;

Setting bmap to NULL is pointless since C doesn't have pass-by-reference ala C++ (i.e., it's not like this function parameter is declared "ext2fs_generic_bitmap &bmap").

Quoted 9 lines

So it's actually a good idea to zap bmap->description, and especially bmap->magic, since that will allow us to catch attempts to use the bitmap after we free it.

I'll make the simplification of your patch which just adds the missing "ext2fs_free_mem(&bmap)" call.

- Ted

Agh, you're right, sorry about that.

Thanks!
-Lukas