From: Alan Cox Date: Sat, 22 Dec 2018 11:17:38 +0000 (+0000) Subject: dup2: Fix two bugs X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=014946e511a68071c45fac3872d0a5ba25e2cc15;p=FUZIX.git 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 --- 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