btrfs-progs: mkfs/rootdir: Fix memory leak in traverse_directory()

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.

Issue: #92
Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
master
Qu Wenruo 2018-02-14 15:50:06 +08:00 committed by David Sterba
parent 54246115ba
commit 78c5a90ebf
2 changed files with 4 additions and 3 deletions

View File

@ -453,7 +453,6 @@ static int traverse_directory(struct btrfs_trans_handle *trans,
ino_t parent_inum, cur_inum; ino_t parent_inum, cur_inum;
ino_t highest_inum = 0; ino_t highest_inum = 0;
const char *parent_dir_name; const char *parent_dir_name;
char real_path[PATH_MAX];
struct btrfs_path path; struct btrfs_path path;
struct extent_buffer *leaf; struct extent_buffer *leaf;
struct btrfs_key root_dir_key; struct btrfs_key root_dir_key;
@ -464,7 +463,7 @@ static int traverse_directory(struct btrfs_trans_handle *trans,
if (!dir_entry) if (!dir_entry)
return -ENOMEM; return -ENOMEM;
dir_entry->dir_name = dir_name; 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) { if (!dir_entry->path) {
error("realpath failed for %s: %s", dir_name, strerror(errno)); error("realpath failed for %s: %s", dir_name, strerror(errno));
ret = -1; ret = -1;
@ -616,6 +615,7 @@ static int traverse_directory(struct btrfs_trans_handle *trans,
} }
free_namelist(files, count); free_namelist(files, count);
free(parent_dir_entry->path);
free(parent_dir_entry); free(parent_dir_entry);
index_cnt = 2; index_cnt = 2;
@ -686,6 +686,7 @@ fail:
dir_entry = list_entry(dir_head.list.next, dir_entry = list_entry(dir_head.list.next,
struct directory_name_entry, list); struct directory_name_entry, list);
list_del(&dir_entry->list); list_del(&dir_entry->list);
free(dir_entry->path);
free(dir_entry); free(dir_entry);
} }
out: out:

View File

@ -23,7 +23,7 @@
struct directory_name_entry { struct directory_name_entry {
const char *dir_name; const char *dir_name;
const char *path; char *path;
ino_t inum; ino_t inum;
struct list_head list; struct list_head list;
}; };