From 43bcac952a63a94ef89b1d4384eee9f1419b61f8 Mon Sep 17 00:00:00 2001 From: guohao15 Date: Wed, 28 Feb 2024 17:17:46 +0800 Subject: [PATCH] tmpfs: old data was loaded when SEEK_SET beyond end of the file Signed-off-by: guohao15 --- fs/tmpfs/fs_tmpfs.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/fs/tmpfs/fs_tmpfs.c b/fs/tmpfs/fs_tmpfs.c index 34f2233630..83d73425a0 100644 --- a/fs/tmpfs/fs_tmpfs.c +++ b/fs/tmpfs/fs_tmpfs.c @@ -281,13 +281,27 @@ static int tmpfs_realloc_file(FAR struct tmpfs_file_s *tfo, * zero. */ - if (newsize > 0) + if (newsize == 0) + { + /* Free the file object */ + + kmm_free(tfo->tfo_data); + tfo->tfo_data = NULL; + tfo->tfo_alloc = 0; + tfo->tfo_size = 0; + return OK; + } + else if (newsize > 0) { /* Otherwise, don't realloc unless the object has shrunk by a * lot. */ delta = tfo->tfo_alloc - newsize; + + /* We should make sure the shrunked memory be zero */ + + memset(tfo->tfo_data + newsize, 0, delta); if (delta <= CONFIG_FS_TMPFS_FILE_FREEGUARD) { /* Hasn't shrunk enough.. Return doing nothing for now */