From 26ceedb5f89a22d49c45ff9b175e251fd14db926 Mon Sep 17 00:00:00 2001 From: Martin Vychodil Date: Tue, 7 Apr 2026 12:08:49 +0200 Subject: [PATCH] fix(fatfs): Fixed uninitialized FATFS pointer for already mounted path --- components/fatfs/host_test/main/test_fatfs_vfs.cpp | 2 +- components/fatfs/vfs/esp_vfs_fat.h | 4 +++- components/fatfs/vfs/vfs_fat.c | 9 +++++++-- components/fatfs/vfs/vfs_fat_bdl.c | 2 +- components/fatfs/vfs/vfs_fat_spiflash.c | 4 ++-- 5 files changed, 14 insertions(+), 7 deletions(-) diff --git a/components/fatfs/host_test/main/test_fatfs_vfs.cpp b/components/fatfs/host_test/main/test_fatfs_vfs.cpp index 33e018906d..7a71878220 100644 --- a/components/fatfs/host_test/main/test_fatfs_vfs.cpp +++ b/components/fatfs/host_test/main/test_fatfs_vfs.cpp @@ -26,7 +26,7 @@ static char drv[3]; static void test_setup(void) { - FATFS *fs; + FATFS *fs = nullptr; esp_err_t ret = ESP_OK; FRESULT fr_result; diff --git a/components/fatfs/vfs/esp_vfs_fat.h b/components/fatfs/vfs/esp_vfs_fat.h index 76df5ff2be..c65196016d 100644 --- a/components/fatfs/vfs/esp_vfs_fat.h +++ b/components/fatfs/vfs/esp_vfs_fat.h @@ -66,7 +66,9 @@ typedef struct { * @param[out] out_fs pointer to FATFS structure which can be used for FATFS f_mount call is returned via this argument. * @return * - ESP_OK on success - * - ESP_ERR_INVALID_STATE if esp_vfs_fat_register was already called + * - ESP_ERR_INVALID_STATE if a filesystem is already registered at this base path. + * If @p out_fs is not NULL, @p *out_fs is set to the existing FATFS object so callers + * can run f_mount (e.g. remount the same path). * - ESP_ERR_NO_MEM if not enough memory or too many VFSes already registered */ esp_err_t esp_vfs_fat_register(const esp_vfs_fat_conf_t* conf, FATFS** out_fs); diff --git a/components/fatfs/vfs/vfs_fat.c b/components/fatfs/vfs/vfs_fat.c index 25229cf0b1..b19f8c10d0 100644 --- a/components/fatfs/vfs/vfs_fat.c +++ b/components/fatfs/vfs/vfs_fat.c @@ -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 */ @@ -179,6 +179,9 @@ esp_err_t esp_vfs_fat_register(const esp_vfs_fat_conf_t* conf, FATFS** out_fs) { size_t ctx = find_context_index_by_path(conf->base_path); if (ctx < FF_VOLUMES) { + if (out_fs) { + *out_fs = &s_fat_ctxs[ctx]->fs; + } return ESP_ERR_INVALID_STATE; } @@ -221,7 +224,9 @@ esp_err_t esp_vfs_fat_register(const esp_vfs_fat_conf_t* conf, FATFS** out_fs) //compatibility s_fat_ctx = fat_ctx; - *out_fs = &fat_ctx->fs; + if (out_fs) { + *out_fs = &fat_ctx->fs; + } return ESP_OK; } diff --git a/components/fatfs/vfs/vfs_fat_bdl.c b/components/fatfs/vfs/vfs_fat_bdl.c index e3adb837bf..ec60d671db 100644 --- a/components/fatfs/vfs/vfs_fat_bdl.c +++ b/components/fatfs/vfs/vfs_fat_bdl.c @@ -116,7 +116,7 @@ esp_err_t esp_vfs_fat_bdl_mount(const char *base_path, ESP_GOTO_ON_ERROR(ff_diskio_register_bdl(pdrv, bdl_handle), fail, TAG, "ff_diskio_register_bdl failed pdrv=%i, error - 0x(%x)", pdrv, ret); - FATFS *fs; + FATFS *fs = NULL; esp_vfs_fat_conf_t conf = { .base_path = base_path, .fat_drive = drv, diff --git a/components/fatfs/vfs/vfs_fat_spiflash.c b/components/fatfs/vfs/vfs_fat_spiflash.c index b1764a049a..8c8cf099af 100644 --- a/components/fatfs/vfs/vfs_fat_spiflash.c +++ b/components/fatfs/vfs/vfs_fat_spiflash.c @@ -171,7 +171,7 @@ esp_err_t esp_vfs_fat_spiflash_mount_rw_wl(const char* base_path, char drv[3] = {(char)('0' + pdrv), ':', 0}; ESP_GOTO_ON_ERROR(ff_diskio_register_wl_partition(pdrv, *wl_handle), fail, TAG, "ff_diskio_register_wl_partition failed pdrv=%i, error - 0x(%x)", pdrv, ret); - FATFS *fs; + FATFS *fs = NULL; esp_vfs_fat_conf_t conf = { .base_path = base_path, .fat_drive = drv, @@ -366,7 +366,7 @@ esp_err_t esp_vfs_fat_spiflash_mount_ro(const char* base_path, char drv[3] = {(char)('0' + pdrv), ':', 0}; ESP_GOTO_ON_ERROR(ff_diskio_register_raw_partition(pdrv, data_partition), fail, TAG, "ff_diskio_register_raw_partition failed pdrv=%i, error - 0x(%x)", pdrv, ret); - FATFS *fs; + FATFS *fs = NULL; esp_vfs_fat_conf_t conf = { .base_path = base_path, .fat_drive = drv,