Skip to content

Commit ab84d31

Browse files
Jiasheng Jiangkdave
authored andcommitted
btrfs: fix memory leaks in create_space_info() error paths
In create_space_info(), the 'space_info' object is allocated at the beginning of the function. However, there are two error paths where the function returns an error code without freeing the allocated memory: 1. When create_space_info_sub_group() fails in zoned mode. 2. When btrfs_sysfs_add_space_info_type() fails. In both cases, 'space_info' has not yet been added to the fs_info->space_info list, resulting in a memory leak. Fix this by adding an error handling label to kfree(space_info) before returning. Fixes: 2be12ef ("btrfs: Separate space_info create/update") Reviewed-by: Qu Wenruo <wqu@suse.com> Signed-off-by: Jiasheng Jiang <jiashengjiangcool@gmail.com> Signed-off-by: David Sterba <dsterba@suse.com>
1 parent 1e4bba7 commit ab84d31

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

fs/btrfs/space-info.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,18 +306,22 @@ static int create_space_info(struct btrfs_fs_info *info, u64 flags)
306306
0);
307307

308308
if (ret)
309-
return ret;
309+
goto out_free;
310310
}
311311

312312
ret = btrfs_sysfs_add_space_info_type(space_info);
313313
if (ret)
314-
return ret;
314+
goto out_free;
315315

316316
list_add(&space_info->list, &info->space_info);
317317
if (flags & BTRFS_BLOCK_GROUP_DATA)
318318
info->data_sinfo = space_info;
319319

320320
return ret;
321+
322+
out_free:
323+
kfree(space_info);
324+
return ret;
321325
}
322326

323327
int btrfs_init_space_info(struct btrfs_fs_info *fs_info)

0 commit comments

Comments
 (0)