All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] btrfs-progs: mkfs/rootdir: Fix memory leak in traverse_directory()
@ 2018-02-14  7:50 Qu Wenruo
  2018-02-14 16:34 ` David Sterba
  0 siblings, 1 reply; 2+ messages in thread
From: Qu Wenruo @ 2018-02-14  7:50 UTC (permalink / raw)
  To: linux-btrfs, dsterba

The bug is exposed by mkfs test case 009, with D=asan.

We are leaking memory of parent_dir_entry->path() which ,except the
rootdir, is allocated by strdup().

Before fixing it, unifiy the allocation of parent_dir_entry() to heap
allocation.

Then fix it by adding "free(parent_dir_entry->path);" in
traverse_directory() and error handler.

Signed-off-by: Qu Wenruo <wqu@suse.com>
---
 mkfs/rootdir.c | 5 +++--
 mkfs/rootdir.h | 2 +-
 2 files changed, 4 insertions(+), 3 deletions(-)

diff --git a/mkfs/rootdir.c b/mkfs/rootdir.c
index 9be4bcbfd0b5..f9472e126674 100644
--- a/mkfs/rootdir.c
+++ b/mkfs/rootdir.c
@@ -453,7 +453,6 @@ static int traverse_directory(struct btrfs_trans_handle *trans,
 	ino_t parent_inum, cur_inum;
 	ino_t highest_inum = 0;
 	const char *parent_dir_name;
-	char real_path[PATH_MAX];
 	struct btrfs_path path;
 	struct extent_buffer *leaf;
 	struct btrfs_key root_dir_key;
@@ -464,7 +463,7 @@ static int traverse_directory(struct btrfs_trans_handle *trans,
 	if (!dir_entry)
 		return -ENOMEM;
 	dir_entry->dir_name = dir_name;
-	dir_entry->path = realpath(dir_name, real_path);
+	dir_entry->path = realpath(dir_name, NULL);
 	if (!dir_entry->path) {
 		error("realpath  failed for %s: %s", dir_name, strerror(errno));
 		ret = -1;
@@ -616,6 +615,7 @@ static int traverse_directory(struct btrfs_trans_handle *trans,
 		}
 
 		free_namelist(files, count);
+		free(parent_dir_entry->path);
 		free(parent_dir_entry);
 
 		index_cnt = 2;
@@ -686,6 +686,7 @@ fail:
 		dir_entry = list_entry(dir_head.list.next,
 				       struct directory_name_entry, list);
 		list_del(&dir_entry->list);
+		free(dir_entry->path);
 		free(dir_entry);
 	}
 out:
diff --git a/mkfs/rootdir.h b/mkfs/rootdir.h
index d0fc2eb58563..f06c7dd16e3c 100644
--- a/mkfs/rootdir.h
+++ b/mkfs/rootdir.h
@@ -23,7 +23,7 @@
 
 struct directory_name_entry {
 	const char *dir_name;
-	const char *path;
+	char *path;
 	ino_t inum;
 	struct list_head list;
 };
-- 
2.16.1


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

* Re: [PATCH] btrfs-progs: mkfs/rootdir: Fix memory leak in traverse_directory()
  2018-02-14  7:50 [PATCH] btrfs-progs: mkfs/rootdir: Fix memory leak in traverse_directory() Qu Wenruo
@ 2018-02-14 16:34 ` David Sterba
  0 siblings, 0 replies; 2+ messages in thread
From: David Sterba @ 2018-02-14 16:34 UTC (permalink / raw)
  To: Qu Wenruo; +Cc: linux-btrfs, dsterba

On Wed, Feb 14, 2018 at 03:50:06PM +0800, Qu Wenruo wrote:
> The bug is exposed by mkfs test case 009, with D=asan.
> 
> We are leaking memory of parent_dir_entry->path() which ,except the
> rootdir, is allocated by strdup().
> 
> Before fixing it, unifiy the allocation of parent_dir_entry() to heap
> allocation.
> 
> Then fix it by adding "free(parent_dir_entry->path);" in
> traverse_directory() and error handler.
> 
> Signed-off-by: Qu Wenruo <wqu@suse.com>

Applied, thanks.

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

end of thread, other threads:[~2018-02-14 16:36 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-02-14  7:50 [PATCH] btrfs-progs: mkfs/rootdir: Fix memory leak in traverse_directory() Qu Wenruo
2018-02-14 16:34 ` David Sterba

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.