Commit e02d48ea authored by Colin Ian King's avatar Colin Ian King Committed by David Sterba

btrfs: fix memory leak of path on error return path

Currently if the allocation of roots or tmp_ulist fails the error handling
does not free up the allocation of path causing a memory leak. Fix this and
other similar leaks by moving the call of btrfs_free_path from label out
to label out_free_ulist.

Kudos to David Sterba for spotting the issue in my original fix and suggesting
the correct way to fix the leak and Anand Jain for spotting a double free
issue.

Addresses-Coverity: ("Resource leak")
Fixes: 5911c8fe ("btrfs: fiemap: preallocate ulists for btrfs_check_shared")
Reviewed-by: default avatarNikolay Borisov <nborisov@suse.com>
Signed-off-by: default avatarColin Ian King <colin.king@canonical.com>
Reviewed-by: default avatarDavid Sterba <dsterba@suse.com>
Signed-off-by: default avatarDavid Sterba <dsterba@suse.com>
parent 28a32d2b
...@@ -4611,7 +4611,6 @@ int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, ...@@ -4611,7 +4611,6 @@ int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
ret = btrfs_lookup_file_extent(NULL, root, path, ret = btrfs_lookup_file_extent(NULL, root, path,
btrfs_ino(BTRFS_I(inode)), -1, 0); btrfs_ino(BTRFS_I(inode)), -1, 0);
if (ret < 0) { if (ret < 0) {
btrfs_free_path(path);
goto out_free_ulist; goto out_free_ulist;
} else { } else {
WARN_ON(!ret); WARN_ON(!ret);
...@@ -4764,11 +4763,11 @@ int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, ...@@ -4764,11 +4763,11 @@ int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
ret = emit_last_fiemap_cache(fieinfo, &cache); ret = emit_last_fiemap_cache(fieinfo, &cache);
free_extent_map(em); free_extent_map(em);
out: out:
btrfs_free_path(path);
unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len - 1, unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len - 1,
&cached_state); &cached_state);
out_free_ulist: out_free_ulist:
btrfs_free_path(path);
ulist_free(roots); ulist_free(roots);
ulist_free(tmp_ulist); ulist_free(tmp_ulist);
return ret; return ret;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment