[PATCH v3] erofs-utils: lib: fix gzran builder memory leak

Ajay Rajera newajay.11r at gmail.com
Sat Mar 21 20:33:04 AEDT 2026


When inflateInit2() fails, erofs_gzran_builder_init() returns
ERR_PTR(-EFAULT) but forgets to free the previously allocated
erofs_gzran_builder struct (gb), resulting in a memory leak.

Fix by calling free(gb) before returning the error.

Signed-off-by: Ajay Rajera <newajay.11r at gmail.com>
---
 lib/gzran.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/lib/gzran.c b/lib/gzran.c
index dffb20a..8a01825 100644
--- a/lib/gzran.c
+++ b/lib/gzran.c
@@ -50,8 +50,10 @@ struct erofs_gzran_builder *erofs_gzran_builder_init(struct erofs_vfile *vf,
 	strm->avail_in = 0;
 	strm->next_in = Z_NULL;
 	ret = inflateInit2(strm, 47);	/* automatic zlib or gzip decoding */
-	if (ret != Z_OK)
+	if (ret != Z_OK) {
+		free(gb);
 		return ERR_PTR(-EFAULT);
+	}
 	gb->vf = vf;
 	gb->span_size = span_size;
 	gb->totout = gb->totin = 0;
-- 
2.51.0.windows.1



More information about the Linux-erofs mailing list