From f57a5a68022684d76c435f8f91fc5d0338d158d3 Mon Sep 17 00:00:00 2001 From: zhangshoukui Date: Fri, 9 May 2025 15:49:36 +0800 Subject: [PATCH] fs/vfs/fs_dup.c: before file_allocate should restore minfd if define FDCHECK MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When we use fcntl for dup, an fd is directly passed. If we have opened FDCHECK. we need to restore this file descriptor. open FDCHECK and test this: ` int main(int ac, char **av) { int fd1= open("./1.txt", O_WRONLY | O_CREAT, 0666); if (fd1 < 0) { printf("open err\n"); return fd1; } int fd2= open("./2.txt", O_WRONLY | O_CREAT, 0666); if (fd2 < 0) { printf("open err\n"); close(fd1); return fd2; } //close(fd2); int fd3 = fcntl(fd1, F_DUPFD, fd2); printf("fd3 = %d\n", fd3); close(fd1); close(fd3); return 0; } ` Signed-off-by: zhangshoukui --- fs/vfs/fs_dup.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/fs/vfs/fs_dup.c b/fs/vfs/fs_dup.c index 3b2492ba4a8..56f0454b876 100644 --- a/fs/vfs/fs_dup.c +++ b/fs/vfs/fs_dup.c @@ -63,6 +63,8 @@ int file_dup(FAR struct file *filep, int minfd, int flags) #ifdef CONFIG_FDCHECK uint8_t f_tag_fdcheck; /* File owner fdcheck tag, init to 0 */ + + minfd = fdcheck_restore(minfd); #endif fd2 = file_allocate(g_root_inode, 0, 0, NULL, minfd, true);