git: 6d49b0c0e7b8 - main - fsck_msdosfs: fix memory leaks in checkfilesys()

From: Xin LI <delphij_at_FreeBSD.org>
Date: Thu, 03 Sep 2026 05:04:01 UTC
The branch main has been updated by delphij:

URL: https://cgit.FreeBSD.org/src/commit/?id=6d49b0c0e7b8b77d8043593356e712307e894743

commit 6d49b0c0e7b8b77d8043593356e712307e894743
Author:     Chris Suter <csuter@google.com>
AuthorDate: 2026-08-04 05:58:25 +0000
Commit:     Xin LI <delphij@FreeBSD.org>
CommitDate: 2026-09-03 04:54:21 +0000

    fsck_msdosfs: fix memory leaks in checkfilesys()
    
    Invoke releasefat(fat) in checkfilesys() prior to free(fat) on exit paths
    so that fatbuf, headbitmap.map, and fat32_cache entries are properly freed.
    
    MFC after:      1 week
    Pull Request:   https://github.com/freebsd/freebsd-src/pull/2351
---
 sbin/fsck_msdosfs/check.c | 5 ++++-
 sbin/fsck_msdosfs/ext.h   | 1 +
 sbin/fsck_msdosfs/fat.c   | 2 +-
 3 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/sbin/fsck_msdosfs/check.c b/sbin/fsck_msdosfs/check.c
index f672a2ac515c..e6c8ba24520f 100644
--- a/sbin/fsck_msdosfs/check.c
+++ b/sbin/fsck_msdosfs/check.c
@@ -181,7 +181,10 @@ checkfilesys(const char *fname)
     out:
 	if (finish_dosdirsection)
 		finishDosDirSection();
-	free(fat);
+	if (fat) {
+		releasefat(fat);
+		free(fat);
+	}
 	close(dosfs);
 
 	if (mod & (FSFATMOD|FSDIRMOD))
diff --git a/sbin/fsck_msdosfs/ext.h b/sbin/fsck_msdosfs/ext.h
index d0f4dd6279f7..80ad684e9622 100644
--- a/sbin/fsck_msdosfs/ext.h
+++ b/sbin/fsck_msdosfs/ext.h
@@ -109,6 +109,7 @@ bool fat_is_valid_cl(struct fat_descriptor *, cl_t);
  * descriptor of it.
  */
 int readfat(int, struct bootblock *, struct fat_descriptor **);
+void releasefat(struct fat_descriptor *);
 
 /*
  * Write back FAT entries
diff --git a/sbin/fsck_msdosfs/fat.c b/sbin/fsck_msdosfs/fat.c
index d71ac2b503f5..7d0ad9a595cf 100644
--- a/sbin/fsck_msdosfs/fat.c
+++ b/sbin/fsck_msdosfs/fat.c
@@ -783,7 +783,7 @@ err:
 	return 0;
 }
 
-static void
+void
 releasefat(struct fat_descriptor *fat)
 {
 	if (fat->is_mmapped) {