From b76ddd9a0aedccbdcfaefec0141b13795cf34893 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Thu, 9 Apr 2026 01:09:38 +0000 Subject: [PATCH] fatfs: prevent dynamic mount buffer leaks on mount failures --- components/fatfs/src/ff.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/components/fatfs/src/ff.c b/components/fatfs/src/ff.c index 29e5048a4e..423753eb6b 100644 --- a/components/fatfs/src/ff.c +++ b/components/fatfs/src/ff.c @@ -3513,8 +3513,9 @@ static FRESULT mount_volume ( /* FR_OK(0): successful, !=0: an error occurred */ if (SS(fs) > FF_MAX_SS || SS(fs) < FF_MIN_SS || (SS(fs) & (SS(fs) - 1))) return FR_DISK_ERR; #endif #if FF_USE_DYN_BUFFER - fs->win = ff_memalloc(SS(fs)); /* Allocate memory for sector buffer */ - if (!fs->win) return FR_NOT_ENOUGH_CORE; + if (fs->win) ff_memfree(fs->win); /* Discard sector buffer left by a failed mount */ + fs->win = ff_memalloc(SS(fs)); /* Allocate memory for sector buffer */ + if (!fs->win) return FR_NOT_ENOUGH_CORE; #endif /* Find an FAT volume on the hosting drive */ @@ -3768,8 +3769,10 @@ FRESULT f_mount ( ff_mutex_delete(vol); #endif #if FF_USE_DYN_BUFFER - if (cfs->fs_type) /* Check if the buffer was ever allocated */ - ff_memfree(cfs->win); /* Deallocate buffer allocated for the filesystem object */ + if (cfs->win) { /* Deallocate buffer allocated for the filesystem object */ + ff_memfree(cfs->win); + cfs->win = 0; + } #endif cfs->fs_type = 0; /* Invalidate the filesystem object to be unregistered */ } @@ -3790,6 +3793,9 @@ FRESULT f_mount ( #endif #endif fs->fs_type = 0; /* Invalidate the new filesystem object */ +#if FF_USE_DYN_BUFFER + fs->win = 0; +#endif FatFs[vol] = fs; /* Register it */ }