fatfs: prevent dynamic mount buffer leaks on mount failures

This commit is contained in:
Cursor Agent
2026-04-09 01:09:38 +00:00
parent 508ba7ae04
commit b76ddd9a0a
+10 -4
View File
@@ -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; if (SS(fs) > FF_MAX_SS || SS(fs) < FF_MIN_SS || (SS(fs) & (SS(fs) - 1))) return FR_DISK_ERR;
#endif #endif
#if FF_USE_DYN_BUFFER #if FF_USE_DYN_BUFFER
fs->win = ff_memalloc(SS(fs)); /* Allocate memory for sector buffer */ if (fs->win) ff_memfree(fs->win); /* Discard sector buffer left by a failed mount */
if (!fs->win) return FR_NOT_ENOUGH_CORE; fs->win = ff_memalloc(SS(fs)); /* Allocate memory for sector buffer */
if (!fs->win) return FR_NOT_ENOUGH_CORE;
#endif #endif
/* Find an FAT volume on the hosting drive */ /* Find an FAT volume on the hosting drive */
@@ -3768,8 +3769,10 @@ FRESULT f_mount (
ff_mutex_delete(vol); ff_mutex_delete(vol);
#endif #endif
#if FF_USE_DYN_BUFFER #if FF_USE_DYN_BUFFER
if (cfs->fs_type) /* Check if the buffer was ever allocated */ if (cfs->win) { /* Deallocate buffer allocated for the filesystem object */
ff_memfree(cfs->win); /* Deallocate buffer allocated for the filesystem object */ ff_memfree(cfs->win);
cfs->win = 0;
}
#endif #endif
cfs->fs_type = 0; /* Invalidate the filesystem object to be unregistered */ cfs->fs_type = 0; /* Invalidate the filesystem object to be unregistered */
} }
@@ -3790,6 +3793,9 @@ FRESULT f_mount (
#endif #endif
#endif #endif
fs->fs_type = 0; /* Invalidate the new filesystem object */ fs->fs_type = 0; /* Invalidate the new filesystem object */
#if FF_USE_DYN_BUFFER
fs->win = 0;
#endif
FatFs[vol] = fs; /* Register it */ FatFs[vol] = fs; /* Register it */
} }