Skip to contentSkip to conversation

two minor fixes for erofs-utils lib

PATCH In review Yifan Zhao 2 participants 8 messages Jul 11, 2025 – Jul 15, 2025 Library code

lore
Share & tools
lore

Yifan Zhao

author
#0 lore raw

cover two minor fixes for erofs-utils lib

Fix following issues during basic usage of mkfs.erofs.

Yifan Zhao (2):
erofs-utils: lib: fix uninitialized variable access in bmgr
erofs-utils: lib: add guard clause for z_erofs_compress_init

 lib/cache.c    | 1 +
 lib/compress.c | 5 +++++
 2 files changed, 6 insertions(+)
Signature · 1 line
2.43.0
author
#1 lore raw

PATCH 1/2 erofs-utils: lib: fix uninitialized variable access in bmgr

Missing bmgr->metablkcnt initialization leads to an uninitialized variable access, fix it.

---
 lib/cache.c | 1 +
 1 file changed, 1 insertion(+)
lib/cache.c +1 -0
index 2c73016..b3cf1c4 100644
@@ -52,6 +52,7 @@ struct erofs_bufmgr *erofs_buffer_init(struct erofs_sb_info *sbi,
 	bmgr->blkh.blkaddr = EROFS_NULL_ADDR;
 	bmgr->tail_blkaddr = startblk;
 	bmgr->last_mapped_block = &bmgr->blkh;
+	bmgr->metablkcnt = 0;
 	bmgr->dsunit = 0;
 	return bmgr;
 }
Signature · 1 line
2.43.0
author
#3 lore raw

PATCH 2/2 erofs-utils: lib: add guard clause for z_erofs_compress_init

In reply to Yifan Zhao

Currently, z_erofs_compress_init allocates heap memory for zmgr even when compression is disabled, causing a memory leak. Let's add a guard clause to skip this allocation.

Fixes a110eea6d80a ("erofs-utils: mkfs: avoid erroring out if `zmgr` is uninitialized")
---
 lib/compress.c | 5 +++++
 1 file changed, 5 insertions(+)
lib/compress.c +5 -0
index bf47121..09b943f 100644
@@ -2027,6 +2027,11 @@ int z_erofs_compress_init(struct erofs_sb_info *sbi, struct erofs_buffer_head *s
 	u32 max_dict_size[Z_EROFS_COMPRESSION_MAX] = {};
 	u32 available_compr_algs = 0;
 
+	/* compression not enabled */
+	if (!cfg.c_compr_opts[0].alg) {
+		return 0;
+	}
+
 	if (!sbi->zmgr) {
 		sbi->zmgr = calloc(1, sizeof(*sbi->zmgr));
 		if (!sbi->zmgr)
Signature · 1 line
2.43.0
#4 lore raw

Re: [PATCH 2/2] erofs-utils: lib: add guard clause for z_erofs_compress_init

Hi Yifan,

Quoted 3 lines · Yifan Zhao

Currently, z_erofs_compress_init allocates heap memory for zmgr even when compression is disabled, causing a memory leak. Let's add a guard clause to skip this allocation.

I think we should free(zmgr) in z_erofs_compress_exit() instead.

allocates zmgr is not a major issue, but I tend to cleanup cfg.c_compr_opts and even cfg soon.

Thanks,
Gao Xiang

author
#5 lore raw

PATCH v2 2/2 erofs-utils: lib: fix memory leak in z_erofs_compress_exit

In reply to Gao Xiang

Currently, z_erofs_compress_exit does not free zmgr if compression is disabled, causing a memory leak. Fix it.

Fixes a110eea6d80a ("erofs-utils: mkfs: avoid erroring out if `zmgr` is uninitialized")

---
change since v1:
- free zmgr in z_erofs_compress_exit rather than allocating it in z_erofs_compress_init conditionally.

 lib/compress.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
lib/compress.c +3 -4
index a57bb6a..3c87a28 100644
@@ -2130,16 +2130,15 @@ int z_erofs_compress_exit(struct erofs_sb_info *sbi)
 {
 	int i, ret;
 
-	/* If `zmgr` is uninitialized, return directly. */
-	if (!sbi->zmgr)
-		return 0;
-
 	for (i = 0; cfg.c_compr_opts[i].alg; ++i) {
 		ret = erofs_compressor_exit(&sbi->zmgr->ccfg[i].handle);
 		if (ret)
 			return ret;
 	}
 
+	if (sbi->zmgr)
+		free(sbi->zmgr);
+
 	if (z_erofs_mt_enabled) {
 #ifdef EROFS_MT_ENABLED
 		ret = erofs_destroy_workqueue(&z_erofs_mt_ctrl.wq);
Signature · 1 line
2.43.0
#6 lore raw

Re: [PATCH v2 2/2] erofs-utils: lib: fix memory leak in z_erofs_compress_exit

Quoted 17 lines · Yifan Zhao

Currently, z_erofs_compress_exit does not free zmgr if compression is disabled, causing a memory leak. Fix it.

Fixes a110eea6d80a ("erofs-utils: mkfs: avoid erroring out if `zmgr` is uninitialized")

---
change since v1:
- free zmgr in z_erofs_compress_exit rather than allocating it in z_erofs_compress_init conditionally.

  lib/compress.c | 7 +++----
  1 file changed, 3 insertions(+), 4 deletions(-)
lib/compress.c +0 -0
index a57bb6a..3c87a28 100644

lib/compress.c int z_erofs_compress_exit(struct erofs_sb_info *sbi) · line 2135

Quoted 6 lines
lib/compress.c +0 -3
@@ -2130,16 +2130,15 @@ int z_erofs_compress_exit(struct erofs_sb_info *sbi)
  {
  	int i, ret;
 
-	/* If `zmgr` is uninitialized, return directly. */
-	if (!sbi->zmgr)
-		return 0;

sbi->zmgr still can be NULL if z_erofs_compress_init() is not called, or sbi->zmgr = calloc(1, sizeof(*sbi->zmgr)); fails.

Thanks,
Gao Xiang

author
#7 lore raw

PATCH v3 2/2 erofs-utils: lib: fix memory leak in z_erofs_compress_exit

Currently, z_erofs_compress_exit does not free zmgr if compression is disabled, causing a memory leak. Fix it.

Fixes a110eea6d80a ("erofs-utils: mkfs: avoid erroring out if `zmgr` is uninitialized")

---
change since v2:
- do not remove early exit in z_erofs_compress_exit.

 lib/compress.c | 3 +++
 1 file changed, 3 insertions(+)
lib/compress.c +3 -0
index a57bb6a..2059e19 100644
@@ -2140,6 +2140,9 @@ int z_erofs_compress_exit(struct erofs_sb_info *sbi)
 			return ret;
 	}
 
+	if (sbi->zmgr)
+		free(sbi->zmgr);
+
 	if (z_erofs_mt_enabled) {
 #ifdef EROFS_MT_ENABLED
 		ret = erofs_destroy_workqueue(&z_erofs_mt_ctrl.wq);
Signature · 1 line
2.43.0