erofs-utils: fix a memory leak of multiple devices
PATCH RESEND 1/1 erofs-utils: fix a memory leak of multiple devices
The memory allocated for multiple devices should be freed when to exit. Let's add a helper to fix it since there is more than one to use it.
--- dump/main.c | 7 ++++--- fsck/main.c | 7 ++++--- fuse/main.c | 5 +++-- include/erofs/internal.h | 1 + lib/super.c | 6 ++++++ 5 files changed, 18 insertions(+), 8 deletions(-)
dump/main.c +4 -3
@@ -615,7 +615,7 @@ int main(int argc, char **argv) err =erofs_read_superblock ();if (err) {erofs_err ("failed to read superblock" ); - goto exit_dev_close; + goto exit_put_super; }if (!dumpcfg.totalshow) {
@@ -630,13 +630,14 @@ int main(int argc, char **argv)if (dumpcfg.show_extent && !dumpcfg.show_inode) {usage (); - goto exit_dev_close; + goto exit_put_super; }if (dumpcfg.show_inode)erofsdump_show_fileinfo (dumpcfg.show_extent); -exit_dev_close: +exit_put_super: +erofs_put_super ();dev_close (); exit:blob_closeall ();
fsck/main.c +4 -3
@@ -813,12 +813,12 @@ int main(int argc, char **argv) err =erofs_read_superblock ();if (err) {erofs_err ("failed to read superblock" ); - goto exit_dev_close; + goto exit_put_super; }if (erofs_sb_has_sb_chksum () &&erofs_check_sb_chksum ()) {erofs_err ("failed to verify superblock checksum" ); - goto exit_dev_close; + goto exit_put_super; } err =erofsfsck_check_inode (sbi.root_nid, sbi.root_nid);
@@ -843,7 +843,8 @@ int main(int argc, char **argv) } } -exit_dev_close: +exit_put_super: +erofs_put_super ();dev_close (); exit:blob_closeall ();
fuse/main.c +3 -2
@@ -295,11 +295,12 @@ int main(int argc, char *argv[]) ret =erofs_read_superblock ();if (ret) {fprintf (stderr,"failed to read erofs super block\n" ); - goto err_dev_close; + goto err_put_super; } ret =fuse_main (args.argc, args.argv, &erofs_ops,NULL ); -err_dev_close: +err_put_super: +erofs_put_super ();blob_closeall ();dev_close (); err_fuse_free_args:
include/erofs/internal.h +1 -0
@@ -318,6 +318,7 @@ struct erofs_map_dev {/* super.c */ int erofs_read_superblock (void ); +void erofs_put_super (void );/* namei.c */ int erofs_read_inode_from_disk (struct erofs_inode *vi);
lib/super.c +6 -0
@@ -109,3 +109,9 @@ int erofs_read_superblock(void)memcpy (&sbi.uuid, dsb->uuid,sizeof (dsb->uuid));return erofs_init_devices (&sbi, dsb); } + +void erofs_put_super (void ) +{ +if (sbi.devs) +free (sbi.devs); +}
Signature · 1 line
2.17.1
Hi Yue,
Quoted 47 lines · Yue Hu
The memory allocated for multiple devices should be freed when to exit. Let's add a helper to fix it since there is more than one to use it.
Signed-off-by Yue Hu <[email protected]>--- dump/main.c | 7 ++++--- fsck/main.c | 7 ++++--- fuse/main.c | 5 +++-- include/erofs/internal.h | 1 + lib/super.c | 6 ++++++ 5 files changed, 18 insertions(+), 8 deletions(-)dump/main.c +4
-3@@ -615,7 +615,7 @@ int main(int argc, char **argv) err =erofs_read_superblock ();if (err) {erofs_err ("failed to read superblock" ); - goto exit_dev_close; + goto exit_put_super; }if (!dumpcfg.totalshow) {@@ -630,13 +630,14 @@ int main(int argc, char **argv)if (dumpcfg.show_extent && !dumpcfg.show_inode) {usage (); - goto exit_dev_close; + goto exit_put_super; }if (dumpcfg.show_inode)erofsdump_show_fileinfo (dumpcfg.show_extent); -exit_dev_close: +exit_put_super: +erofs_put_super ();dev_close (); exit:blob_closeall ();fsck/main.c +0
-0
fsck/main.c int main(int argc, char **argv) · line 816
Quoted 5 lines
fsck/main.c +1
-1@@ -813,12 +813,12 @@ int main(int argc, char **argv) err =erofs_read_superblock ();if (err) {erofs_err ("failed to read superblock" ); - goto exit_dev_close; + goto exit_put_super;
Why do we call erofs_put_super() again here? I think we don't need to call erofs_put_super for all failed paths.
Thanks,
Gao Xiang
Quoted 50 lines · On Fri, 22 Jul 2022 11:28:27 +0800 Gao Xiang <[email protected]>…
Hi Yue,
Quoted 47 lines · Yue Hu
The memory allocated for multiple devices should be freed when to exit. Let's add a helper to fix it since there is more than one to use it.
Signed-off-by Yue Hu <[email protected]>--- dump/main.c | 7 ++++--- fsck/main.c | 7 ++++--- fuse/main.c | 5 +++-- include/erofs/internal.h | 1 + lib/super.c | 6 ++++++ 5 files changed, 18 insertions(+), 8 deletions(-)dump/main.c +4
-3@@ -615,7 +615,7 @@ int main(int argc, char **argv) err =erofs_read_superblock ();if (err) {erofs_err ("failed to read superblock" ); - goto exit_dev_close; + goto exit_put_super; }if (!dumpcfg.totalshow) {@@ -630,13 +630,14 @@ int main(int argc, char **argv)if (dumpcfg.show_extent && !dumpcfg.show_inode) {usage (); - goto exit_dev_close; + goto exit_put_super; }if (dumpcfg.show_inode)erofsdump_show_fileinfo (dumpcfg.show_extent); -exit_dev_close: +exit_put_super: +erofs_put_super ();dev_close (); exit:blob_closeall ();fsck/main.c +0
-0@@ -813,12 +813,12 @@ int main(int argc, char **argv)
Quoted 8 lines
fsck/main.c int main(int argc, char **argv) · line 816
Quoted 5 lines
fsck/main.c +1
-1err =erofs_read_superblock ();if (err) {erofs_err ("failed to read superblock" ); - goto exit_dev_close; + goto exit_put_super;Why do we call
erofs_put_super()again here? I think we don't need to callerofs_put_superfor all failed paths.
There is a call to dev_read() which may fails after allocating memory. Let me send v2 for this.
Quoted 3 lines
Thanks,
Gao Xiang