All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/2] two minor fixes for erofs-utils lib
@ 2025-07-11 16:16 Yifan Zhao
  2025-07-11 16:16 ` [PATCH 1/2] erofs-utils: lib: fix uninitialized variable access in bmgr Yifan Zhao
  2025-07-11 16:16 ` [PATCH 2/2] erofs-utils: lib: add guard clause for z_erofs_compress_init Yifan Zhao
  0 siblings, 2 replies; 8+ messages in thread
From: Yifan Zhao @ 2025-07-11 16:16 UTC (permalink / raw)
  To: linux-erofs; +Cc: Yifan Zhao

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(+)

-- 
2.43.0



^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH 1/2] erofs-utils: lib: fix uninitialized variable access in bmgr
  2025-07-11 16:16 [PATCH 0/2] two minor fixes for erofs-utils lib Yifan Zhao
@ 2025-07-11 16:16 ` Yifan Zhao
  2025-07-11 16:40   ` Gao Xiang
  2025-07-11 16:16 ` [PATCH 2/2] erofs-utils: lib: add guard clause for z_erofs_compress_init Yifan Zhao
  1 sibling, 1 reply; 8+ messages in thread
From: Yifan Zhao @ 2025-07-11 16:16 UTC (permalink / raw)
  To: linux-erofs; +Cc: Yifan Zhao

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

Signed-off-by: Yifan Zhao <stopire@gmail.com>
---
 lib/cache.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/lib/cache.c b/lib/cache.c
index 2c73016..b3cf1c4 100644
--- a/lib/cache.c
+++ b/lib/cache.c
@@ -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;
 }
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH 2/2] erofs-utils: lib: add guard clause for z_erofs_compress_init
  2025-07-11 16:16 [PATCH 0/2] two minor fixes for erofs-utils lib Yifan Zhao
  2025-07-11 16:16 ` [PATCH 1/2] erofs-utils: lib: fix uninitialized variable access in bmgr Yifan Zhao
@ 2025-07-11 16:16 ` Yifan Zhao
  2025-07-11 16:39   ` Gao Xiang
  1 sibling, 1 reply; 8+ messages in thread
From: Yifan Zhao @ 2025-07-11 16:16 UTC (permalink / raw)
  To: linux-erofs; +Cc: 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")
Signed-off-by: Yifan Zhao <stopire@gmail.com>
---
 lib/compress.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/lib/compress.c b/lib/compress.c
index bf47121..09b943f 100644
--- a/lib/compress.c
+++ b/lib/compress.c
@@ -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)
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH 2/2] erofs-utils: lib: add guard clause for z_erofs_compress_init
  2025-07-11 16:16 ` [PATCH 2/2] erofs-utils: lib: add guard clause for z_erofs_compress_init Yifan Zhao
@ 2025-07-11 16:39   ` Gao Xiang
  2025-07-14 16:55     ` [PATCH v2 2/2] erofs-utils: lib: fix memory leak in z_erofs_compress_exit Yifan Zhao
  0 siblings, 1 reply; 8+ messages in thread
From: Gao Xiang @ 2025-07-11 16:39 UTC (permalink / raw)
  To: Yifan Zhao, linux-erofs

Hi Yifan,

On 2025/7/12 00:16, Yifan Zhao wrote:
> 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


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 1/2] erofs-utils: lib: fix uninitialized variable access in bmgr
  2025-07-11 16:16 ` [PATCH 1/2] erofs-utils: lib: fix uninitialized variable access in bmgr Yifan Zhao
@ 2025-07-11 16:40   ` Gao Xiang
  0 siblings, 0 replies; 8+ messages in thread
From: Gao Xiang @ 2025-07-11 16:40 UTC (permalink / raw)
  To: Yifan Zhao, linux-erofs



On 2025/7/12 00:16, Yifan Zhao wrote:
> Missing `bmgr->metablkcnt` initialization leads to an uninitialized
> variable access, fix it.
> 
> Signed-off-by: Yifan Zhao <stopire@gmail.com>

Reviewed-by: Gao Xiang <hsiangkao@linux.alibaba.com>

Thanks,
Gao Xiang


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v2 2/2] erofs-utils: lib: fix memory leak in z_erofs_compress_exit
  2025-07-11 16:39   ` Gao Xiang
@ 2025-07-14 16:55     ` Yifan Zhao
  2025-07-14 17:09       ` Gao Xiang
  0 siblings, 1 reply; 8+ messages in thread
From: Yifan Zhao @ 2025-07-14 16:55 UTC (permalink / raw)
  To: linux-erofs; +Cc: 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")
Signed-off-by: Yifan Zhao <stopire@gmail.com>
---
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(-)

diff --git a/lib/compress.c b/lib/compress.c
index a57bb6a..3c87a28 100644
--- a/lib/compress.c
+++ b/lib/compress.c
@@ -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);
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH v2 2/2] erofs-utils: lib: fix memory leak in z_erofs_compress_exit
  2025-07-14 16:55     ` [PATCH v2 2/2] erofs-utils: lib: fix memory leak in z_erofs_compress_exit Yifan Zhao
@ 2025-07-14 17:09       ` Gao Xiang
  2025-07-15 15:21         ` [PATCH v3 " Yifan Zhao
  0 siblings, 1 reply; 8+ messages in thread
From: Gao Xiang @ 2025-07-14 17:09 UTC (permalink / raw)
  To: Yifan Zhao, linux-erofs



On 2025/7/15 00:55, Yifan Zhao wrote:
> 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")
> Signed-off-by: Yifan Zhao <stopire@gmail.com>
> ---
> 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(-)
> 
> diff --git a/lib/compress.c b/lib/compress.c
> index a57bb6a..3c87a28 100644
> --- a/lib/compress.c
> +++ b/lib/compress.c
> @@ -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


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v3 2/2] erofs-utils: lib: fix memory leak in z_erofs_compress_exit
  2025-07-14 17:09       ` Gao Xiang
@ 2025-07-15 15:21         ` Yifan Zhao
  0 siblings, 0 replies; 8+ messages in thread
From: Yifan Zhao @ 2025-07-15 15:21 UTC (permalink / raw)
  To: linux-erofs; +Cc: 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")
Signed-off-by: Yifan Zhao <stopire@gmail.com>
---
change since v2:
- do not remove early exit in `z_erofs_compress_exit`.

 lib/compress.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/lib/compress.c b/lib/compress.c
index a57bb6a..2059e19 100644
--- a/lib/compress.c
+++ b/lib/compress.c
@@ -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);
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2025-07-15 15:21 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-07-11 16:16 [PATCH 0/2] two minor fixes for erofs-utils lib Yifan Zhao
2025-07-11 16:16 ` [PATCH 1/2] erofs-utils: lib: fix uninitialized variable access in bmgr Yifan Zhao
2025-07-11 16:40   ` Gao Xiang
2025-07-11 16:16 ` [PATCH 2/2] erofs-utils: lib: add guard clause for z_erofs_compress_init Yifan Zhao
2025-07-11 16:39   ` Gao Xiang
2025-07-14 16:55     ` [PATCH v2 2/2] erofs-utils: lib: fix memory leak in z_erofs_compress_exit Yifan Zhao
2025-07-14 17:09       ` Gao Xiang
2025-07-15 15:21         ` [PATCH v3 " Yifan Zhao

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.