f2fs-tools: reuse same pointer, exit on error without clean-up

This patch tries to fix memory leak problem reported in Android.

Fixed the following problems in fsck.f2fs, make_f2fs and sload_f2fs:
    * reuse of same pointer without clean-up
    * exit on error without clean-up

Signed-off-by: Robin Hsu <robinhsu@google.com>
[Jaegeuk Kim: add missing definition to avoid build error]
Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
This commit is contained in:
Robin Hsu 2019-10-31 21:57:59 +08:00 committed by Jaegeuk Kim
parent 17a7e7e0f0
commit 27be95ee8f
5 changed files with 50 additions and 27 deletions

View File

@ -773,11 +773,14 @@ int main(int argc, char **argv)
f2fs_parse_options(argc, argv);
if (c.func != DUMP && f2fs_devs_are_umounted() < 0) {
if (errno == EBUSY)
return -1;
if (errno == EBUSY) {
ret = -1;
goto quick_err;
}
if (!c.ro || c.func == DEFRAG) {
MSG(0, "\tError: Not available on mounted device!\n");
return -1;
ret = -1;
goto quick_err;
}
/* allow ro-mounted partition */
@ -791,8 +794,10 @@ int main(int argc, char **argv)
}
/* Get device */
if (f2fs_get_device_info() < 0)
return -1;
if (f2fs_get_device_info() < 0) {
ret = -1;
goto quick_err;
}
fsck_again:
memset(&gfsck, 0, sizeof(gfsck));
@ -883,5 +888,7 @@ out_err:
free(sbi->ckpt);
if (sbi->raw_super)
free(sbi->raw_super);
quick_err:
f2fs_release_sparse_resource();
return ret;
}

View File

@ -98,7 +98,8 @@ static void write_all_xattrs(struct f2fs_sb_info *sbi,
xattr_node = calloc(BLOCK_SZ, 1);
ASSERT(xattr_node);
ret = dev_read_block(xattr_node, ni.blk_addr);
ASSERT(ret >= 0);
if (ret < 0)
goto free_xattr_node;
}
/* write to xattr node block */
@ -107,10 +108,10 @@ static void write_all_xattrs(struct f2fs_sb_info *sbi,
PAGE_SIZE - sizeof(struct node_footer));
ret = dev_write_block(xattr_node, blkaddr);
ASSERT(ret >= 0);
if (xnid)
free(xattr_node);
free_xattr_node:
free(xattr_node);
ASSERT(ret >= 0);
}
int f2fs_setxattr(struct f2fs_sb_info *sbi, nid_t ino, int index, const char *name,

View File

@ -1185,6 +1185,7 @@ extern int f2fs_get_device_info(void);
extern unsigned int calc_extra_isize(void);
extern int get_device_info(int);
extern int f2fs_init_sparse_file(void);
extern void f2fs_release_sparse_resource(void);
extern int f2fs_finalize_device(void);
extern int f2fs_fsync_device(void);

View File

@ -311,6 +311,8 @@ int f2fs_init_sparse_file(void)
#ifdef WITH_ANDROID
if (c.func == MKFS) {
f2fs_sparse_file = sparse_file_new(F2FS_BLKSIZE, c.device_size);
if (!f2fs_sparse_file)
return -1;
} else {
f2fs_sparse_file = sparse_file_import(c.devices[0].fd,
true, false);
@ -346,6 +348,26 @@ int f2fs_init_sparse_file(void)
#endif
}
void f2fs_release_sparse_resource(void)
{
#ifdef WITH_ANDROID
int j;
if (c.sparse_mode) {
if (f2fs_sparse_file != NULL) {
sparse_file_destroy(f2fs_sparse_file);
f2fs_sparse_file = NULL;
}
for (j = 0; j < blocks_count; j++)
free(blocks[j]);
free(blocks);
blocks = NULL;
free(zeroed_block);
zeroed_block = NULL;
}
#endif
}
#define MAX_CHUNK_SIZE (1 * 1024 * 1024 * 1024ULL)
#define MAX_CHUNK_COUNT (MAX_CHUNK_SIZE / F2FS_BLKSIZE)
int f2fs_finalize_device(void)
@ -412,14 +434,7 @@ int f2fs_finalize_device(void)
sparse_file_write(f2fs_sparse_file, c.devices[0].fd,
/*gzip*/0, /*sparse*/1, /*crc*/0);
sparse_file_destroy(f2fs_sparse_file);
for (j = 0; j < blocks_count; j++)
free(blocks[j]);
free(blocks);
blocks = NULL;
free(zeroed_block);
zeroed_block = NULL;
f2fs_sparse_file = NULL;
f2fs_release_sparse_resource();
}
#endif
/*

View File

@ -361,7 +361,7 @@ int main(int argc, char *argv[])
}
if (f2fs_get_device_info() < 0)
return -1;
goto err_format;
/*
* Some options are mandatory for host-managed
@ -369,26 +369,25 @@ int main(int argc, char *argv[])
*/
if (c.zoned_model == F2FS_ZONED_HM && !c.zoned_mode) {
MSG(0, "\tError: zoned block device feature is required\n");
return -1;
goto err_format;
}
if (c.zoned_mode && !c.trim) {
MSG(0, "\tError: Trim is required for zoned block devices\n");
return -1;
}
if (c.sparse_mode) {
if (f2fs_init_sparse_file())
return -1;
goto err_format;
}
if (f2fs_format_device() < 0)
return -1;
goto err_format;
if (f2fs_finalize_device() < 0)
return -1;
goto err_format;
MSG(0, "Info: format successful\n");
return 0;
err_format:
f2fs_release_sparse_resource();
return -1;
}