fix(fatfs): fix f_getfree crash when volume not mounted or mount failed

This commit is contained in:
sonika.rathi
2026-05-12 01:43:41 +08:00
committed by BOT
parent d3d57c1cf4
commit c01c7104ca
2 changed files with 18 additions and 4 deletions
+11 -3
View File
@@ -138,7 +138,10 @@ void ff_mutex_delete ( /* Returns 1:Function succeeded or 0:Could not delete due
OSMutexDel(Mutex[vol], OS_DEL_ALWAYS, &err);
#elif OS_TYPE == 3 /* FreeRTOS */
vSemaphoreDelete(Mutex[vol]);
if (Mutex[vol] != NULL) {
vSemaphoreDelete(Mutex[vol]);
Mutex[vol] = NULL; /* Prevent use-after-delete in ff_mutex_take/ff_mutex_give */
}
#elif OS_TYPE == 4 /* CMSIS-RTOS */
osMutexDelete(Mutex[vol]);
@@ -167,10 +170,13 @@ int ff_mutex_take ( /* Returns 1:Succeeded or 0:Timeout */
#elif OS_TYPE == 2 /* uC/OS-II */
OS_ERR err;
OSMutexPend(Mutex[vol], FF_FS_TIMEOUT, &err));
OSMutexPend(Mutex[vol], FF_FS_TIMEOUT, &err);
return (int)(err == OS_NO_ERR);
#elif OS_TYPE == 3 /* FreeRTOS */
if (vol < 0 || vol > FF_VOLUMES || Mutex[vol] == NULL) {
return 0; /* No volume mounted or mutex not created -> treat as timeout */
}
return (int)(xSemaphoreTake(Mutex[vol], FF_FS_TIMEOUT) == pdTRUE);
#elif OS_TYPE == 4 /* CMSIS-RTOS */
@@ -201,7 +207,9 @@ void ff_mutex_give (
OSMutexPost(Mutex[vol]);
#elif OS_TYPE == 3 /* FreeRTOS */
xSemaphoreGive(Mutex[vol]);
if (vol >= 0 && vol <= FF_VOLUMES && Mutex[vol] != NULL) {
xSemaphoreGive(Mutex[vol]);
}
#elif OS_TYPE == 4 /* CMSIS-RTOS */
osMutexRelease(Mutex[vol]);
+7 -1
View File
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2015-2026 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@@ -215,6 +215,11 @@ esp_err_t esp_vfs_fat_spiflash_mount_rw_wl(const char* base_path,
return ESP_OK;
fail:
/* Unmount FatFs volume if we had registered and attempted mount (e.g. s_f_mount_rw
* failed). Otherwise FatFs[vol] can be left set with a dangling pointer after
* esp_vfs_fat_unregister_path frees the context, and the volume mutex stays
* created; a later f_getfree then crashes in lock_volume / ff_mutex_take. */
f_mount(0, drv, 0);
esp_vfs_fat_unregister_path(base_path);
ff_diskio_unregister(pdrv);
free(ctx);
@@ -390,6 +395,7 @@ esp_err_t esp_vfs_fat_spiflash_mount_ro(const char* base_path,
return ESP_OK;
fail:
f_mount(0, drv, 0); /* Unmount on failed mount so FatFs[vol] and mutex are cleaned up */
esp_vfs_fat_unregister_path(base_path);
ff_diskio_unregister(pdrv);
return ret;