From 014946e511a68071c45fac3872d0a5ba25e2cc15 Mon Sep 17 00:00:00 2001 From: Alan Cox Date: Sat, 22 Dec 2018 11:17:38 +0000 Subject: [PATCH] dup2: Fix two bugs 1. dup2(x,x) corrupted all the counts we closed the object then referenced it 2. dup2() returns the new file handle not 0 on success --- Kernel/syscall_fs.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/Kernel/syscall_fs.c b/Kernel/syscall_fs.c index 60ea8c97..5e9d2e9b 100644 --- a/Kernel/syscall_fs.c +++ b/Kernel/syscall_fs.c @@ -206,20 +206,24 @@ arg_t _dup2(void) { if (getinode(oldd) == NULLINODE) - return (-1); + return -1; if (newd < 0 || newd >= UFTSIZE) { udata.u_error = EBADF; - return (-1); + return -1; } + /* No-op - but we must not close and dup so catch it */ + if (newd == oldd) + return oldd; + if (udata.u_files[newd] != NO_FILE) doclose(newd); udata.u_files[newd] = udata.u_files[oldd]; ++of_tab[udata.u_files[oldd]].o_refs; - return (0); + return newd; } #undef oldd -- 2.34.1