From cd9c1c0c31aa8047a363848aa4b54289949c367b Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Mon, 18 Dec 2023 00:43:22 -0600 Subject: [PATCH] Fixed file buffers falling out-of-date when bypassed during writes This can happend if we read some data into our buffer (or write+flush+seek in some weird pattern) and then do a bypassing write that overlaps the buffer. The solution here is to make sure the buffer is cleared when doing bypassing writes. In theory, we could be a bit smarter by checking and only clearing when overlap occurs, or even updating the buffer with the new contents like we do in the bd cache layer, but the value would probably be minimal. File writes are already expected to clobber buffered reads. --- lfs.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lfs.c b/lfs.c index b94b7b00..18160865 100644 --- a/lfs.c +++ b/lfs.c @@ -10478,7 +10478,13 @@ lfs_ssize_t lfsr_file_write(lfs_t *lfs, lfsr_file_t *file, // strictly necessary, but enforces a more intuitive write order // and avoids weird cases with low-level write heuristics // + // oh, and also avoids issues with out-of-date buffers + // if (!unflushed_ && size >= lfs->cfg->cache_size) { + // clear buffer to avoid out-of-date data + buffer_pos_ = 0; + buffer_size_ = 0; + err = lfsr_ftree_flush(lfs, &file->mdir, &ftree_, pos_, buffer_, size); if (err) {