From 2fa3b72b3cd0f8ff93c673dfde3276985537fa3c Mon Sep 17 00:00:00 2001 From: SPRESENSE <41312067+SPRESENSE@users.noreply.github.com> Date: Thu, 12 Jun 2025 11:57:06 +0900 Subject: [PATCH] fs/smartfs: Fix a fatal bug about sector writing after seek When writing to the next sector after the forward position has been written by seek, the old sector buffer is used, which may corrupt the file system. Therefore, the sector buffer must always be updated after a writing by seek. Signed-off-by: SPRESENSE <41312067+SPRESENSE@users.noreply.github.com> --- fs/smartfs/smartfs_smart.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/fs/smartfs/smartfs_smart.c b/fs/smartfs/smartfs_smart.c index 2317f967cd..3af3a6341f 100644 --- a/fs/smartfs/smartfs_smart.c +++ b/fs/smartfs/smartfs_smart.c @@ -796,6 +796,28 @@ static ssize_t smartfs_write(FAR struct file *filep, FAR const char *buffer, } } +#ifdef CONFIG_SMARTFS_USE_SECTOR_BUFFER + + /* If data is written to a forward position using seek, the sector + * buffer must be updated because it may be referenced later. + */ + + if (byteswritten > 0) + { + readwrite.logsector = sf->currsector; + readwrite.offset = 0; + readwrite.count = fs->fs_llformat.availbytes; + readwrite.buffer = (FAR uint8_t *)sf->buffer; + ret = FS_IOCTL(fs, BIOC_READSECT, (unsigned long)&readwrite); + if (ret < 0) + { + ferr("ERROR: Error %d reading sector %d\n", ret, sf->currsector); + goto errout_with_lock; + } + } + +#endif /* CONFIG_SMARTFS_USE_SECTOR_BUFFER */ + /* Now append data to end of the file. */ while (buflen > 0)