dup2: Fix two bugs
authorAlan Cox <alan@linux.intel.com>
Sat, 22 Dec 2018 11:17:38 +0000 (11:17 +0000)
committerAlan Cox <alan@linux.intel.com>
Sat, 22 Dec 2018 11:17:38 +0000 (11:17 +0000)
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

index 60ea8c9..5e9d2e9 100644 (file)
@@ -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