[f2fs-dev] [PATCH 1/3] fsck.f2fs: fix to avoid memory leak reported by LeakSanitizer

Chao Yu via Linux-f2fs-devel <[email protected]> Sat, 15 Aug 2026 08:54:38 +0800
Newsgroups net.sourceforge.lists.linux-f2fs-devel
Message-ID <[email protected]>
==5957==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 4096 byte(s) in 1 object(s) allocated from:
    #0 0x7f08b78bc340 in calloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:77
    #1 0x55b9e0fcbcc7 in update_data_blkaddr /share/tools-f2fs/fsck/mount.c:2523

SUMMARY: AddressSanitizer: 4096 byte(s) leaked in 1 allocation(s).

When a process calls exit(), the C runtime executes all registered
atexit handlers.
AddressSanitizer / LeakSanitizer (LSan) hooks into exit() to inspect
the heap, it checks whether all memory allocated via malloc() / calloc()
was explicitly released.

Relocate sanity check and ASSERT(0) before node block allocation to fix
this issue.

Signed-off-by: Chao Yu <[email protected]>
---
 fsck/mount.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/fsck/mount.c b/fsck/mount.c
index 2e7ea45..26f213e 100644
--- a/fsck/mount.c
+++ b/fsck/mount.c
@@ -2520,22 +2520,22 @@ void update_data_blkaddr(struct f2fs_sb_info *sbi, nid_t nid,
 	if (node_blk == NULL) {
 		struct seg_entry *se;
 
-		node_blk = (struct f2fs_node *)calloc(F2FS_BLKSIZE, 1);
-		ASSERT(node_blk);
-
 		get_node_info(sbi, nid, &ni);
 
-		/* read node_block */
-		ret = dev_read_block(node_blk, ni.blk_addr);
-		ASSERT(ret >= 0);
-		node_blk_alloced = true;
-
 		se = get_seg_entry(sbi, GET_SEGNO(sbi, ni.blk_addr));
 		if (IS_DATASEG(se->type)) {
 			ERR_MSG("NAT and SIT is inconsistent: ino: %u, nid: %u, blkaddr: %u, segtype: %d",
 				ni.ino, ni.nid, ni.blk_addr, se->type);
 			ASSERT(0);
 		}
+
+		node_blk = (struct f2fs_node *)calloc(F2FS_BLKSIZE, 1);
+		ASSERT(node_blk);
+
+		/* read node_block */
+		ret = dev_read_block(node_blk, ni.blk_addr);
+		ASSERT(ret >= 0);
+		node_blk_alloced = true;
 	}
 
 	/* check its block address */
-- 
2.49.0



_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel