erofs-utils: mkfs: fix potential memory leak
authorYifan Zhao <zhaoyifan@sjtu.edu.cn>
Sat, 4 Nov 2023 06:50:41 +0000 (14:50 +0800)
committerGao Xiang <hsiangkao@linux.alibaba.com>
Mon, 6 Nov 2023 05:40:12 +0000 (13:40 +0800)
Valgrind reports 2 potential memory leaks in mkfs:

    Command: mkfs.erofs -zlz4 test.img testdir/

    4 bytes in 1 blocks are still reachable in loss record 1 of 2
       at 0x4841848: malloc (vg_replace_malloc.c:431)
       by 0x49633DE: strdup (strdup.c:42)
       by 0x10C483: mkfs_parse_compress_algs (main.c:287)
       by 0x10C483: mkfs_parse_options_cfg (main.c:316)
       by 0x10C483: main (main.c:936)

    34 bytes in 1 blocks are still reachable in loss record 2 of 2
       at 0x4841848: malloc (vg_replace_malloc.c:431)
       by 0x49633DE: strdup (strdup.c:42)
       by 0x48FFE2B: realpath_stk (canonicalize.c:409)
       by 0x48FFE2B: realpath@@GLIBC_2.3 (canonicalize.c:431)
       by 0x10B7EB: mkfs_parse_options_cfg (main.c:587)
       by 0x10B7EB: main (main.c:936)

Fix it by freeing the memory allocated by strdup() and realpath().

Signed-off-by: Yifan Zhao <zhaoyifan@sjtu.edu.cn>
Reviewed-by: Gao Xiang <hsiangkao@linux.alibaba.com>
Link: https://lore.kernel.org/r/20231104065041.129680-1-zhaoyifan@sjtu.edu.cn
Signed-off-by: Gao Xiang <hsiangkao@linux.alibaba.com>
lib/config.c

index a3235c8496989c7ea96bee2aa4f417789e64ec38..0cddc95d61b7cbc419e72d0a86abf8dda3574cfa 100644 (file)
@@ -48,12 +48,18 @@ void erofs_show_config(void)
 
 void erofs_exit_configure(void)
 {
+       int i;
+
 #ifdef HAVE_LIBSELINUX
        if (cfg.sehnd)
                selabel_close(cfg.sehnd);
 #endif
        if (cfg.c_img_path)
                free(cfg.c_img_path);
+       if (cfg.c_src_path)
+               free(cfg.c_src_path);
+       for (i = 0; i < EROFS_MAX_COMPR_CFGS && cfg.c_compr_alg[i]; i++)
+               free(cfg.c_compr_alg[i]);
 }
 
 static unsigned int fullpath_prefix;   /* root directory prefix length */