From b1c4107275a91e0ec9b5cc175adf445f24d23885 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Thu, 5 Nov 2020 17:38:22 +0100 Subject: [PATCH] vfs: zero-initialize struct stat in *_stat and *_fstat handlers ...otherwise some fields will contain garbage values. This wasn't noticed until HAVE_BLKSIZE got enabled in newlib builds. --- components/fatfs/vfs/vfs_fat.c | 1 + components/spiffs/esp_spiffs.c | 3 ++- components/tinyusb/additions/src/vfs_tinyusb.c | 1 + components/vfs/vfs_cdcacm.c | 1 + components/vfs/vfs_uart.c | 2 +- 5 files changed, 6 insertions(+), 2 deletions(-) diff --git a/components/fatfs/vfs/vfs_fat.c b/components/fatfs/vfs/vfs_fat.c index 7e9f9950f4..3e21bd98a8 100644 --- a/components/fatfs/vfs/vfs_fat.c +++ b/components/fatfs/vfs/vfs_fat.c @@ -552,6 +552,7 @@ static int vfs_fat_fstat(void* ctx, int fd, struct stat * st) { vfs_fat_ctx_t* fat_ctx = (vfs_fat_ctx_t*) ctx; FIL* file = &fat_ctx->files[fd]; + memset(st, 0, sizeof(*st)); st->st_size = f_size(file); st->st_mode = S_IRWXU | S_IRWXG | S_IRWXO | S_IFREG; st->st_mtime = 0; diff --git a/components/spiffs/esp_spiffs.c b/components/spiffs/esp_spiffs.c index 92ef3aa18d..e5cc88daf7 100644 --- a/components/spiffs/esp_spiffs.c +++ b/components/spiffs/esp_spiffs.c @@ -536,6 +536,7 @@ static int vfs_spiffs_fstat(void* ctx, int fd, struct stat * st) SPIFFS_clearerr(efs->fs); return -1; } + memset(st, 0, sizeof(*st)); st->st_size = s.size; st->st_mode = S_IRWXU | S_IRWXG | S_IRWXO | S_IFREG; st->st_mtime = vfs_spiffs_get_mtime(&s); @@ -558,7 +559,7 @@ static int vfs_spiffs_stat(void* ctx, const char * path, struct stat * st) SPIFFS_clearerr(efs->fs); return -1; } - + memset(st, 0, sizeof(*st)); st->st_size = s.size; st->st_mode = S_IRWXU | S_IRWXG | S_IRWXO; st->st_mode |= (s.type == SPIFFS_TYPE_DIR)?S_IFDIR:S_IFREG; diff --git a/components/tinyusb/additions/src/vfs_tinyusb.c b/components/tinyusb/additions/src/vfs_tinyusb.c index bc84711af7..2348405554 100644 --- a/components/tinyusb/additions/src/vfs_tinyusb.c +++ b/components/tinyusb/additions/src/vfs_tinyusb.c @@ -207,6 +207,7 @@ static ssize_t tusb_read(int fd, void *data, size_t size) static int tusb_fstat(int fd, struct stat *st) { FD_CHECK(fd, -1); + memset(st, 0, sizeof(*st)); st->st_mode = S_IFCHR; return 0; } diff --git a/components/vfs/vfs_cdcacm.c b/components/vfs/vfs_cdcacm.c index 02cce7e681..cd14d212dd 100644 --- a/components/vfs/vfs_cdcacm.c +++ b/components/vfs/vfs_cdcacm.c @@ -96,6 +96,7 @@ static int cdcacm_open(const char *path, int flags, int mode) static int cdcacm_fstat(int fd, struct stat *st) { assert(fd == 0); + memset(st, 0, sizeof(*st)); st->st_mode = S_IFCHR; return 0; } diff --git a/components/vfs/vfs_uart.c b/components/vfs/vfs_uart.c index 9ab62fccc7..1d843b47fc 100644 --- a/components/vfs/vfs_uart.c +++ b/components/vfs/vfs_uart.c @@ -291,7 +291,7 @@ static ssize_t uart_read(int fd, void* data, size_t size) static int uart_fstat(int fd, struct stat * st) { assert(fd >=0 && fd < 3); - st->st_blksize = BUFSIZ; + memset(st, 0, sizeof(*st)); st->st_mode = S_IFCHR; return 0; }