Skip to content

Commit 35d8e02

Browse files
pcercueiQuzarDC
authored andcommitted
fs: Handle case where old FD == new FD in fs_dup2()
Previously, when the same FD was passed as both the new and old FD to fs_dup2(), the algorithm would close the FD then try to get a reference to a NULL pointer. Address this issue by following the POSIX spec, and return the FD directly, with its reference count incremented. Signed-off-by: Paul Cercueil <paul@crapouillou.net>
1 parent 607d1a8 commit 35d8e02

1 file changed

Lines changed: 4 additions & 0 deletions

File tree

kernel/fs/fs.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,9 @@ file_t fs_dup2(file_t oldfd, file_t newfd) {
309309
return -1;
310310
}
311311

312+
if(oldfd == newfd)
313+
goto out_get_ref;
314+
312315
do {
313316
prev = fd_table[newfd];
314317
if(prev) {
@@ -319,6 +322,7 @@ file_t fs_dup2(file_t oldfd, file_t newfd) {
319322
} while(!atomic_compare_exchange_strong(&fd_table[newfd],
320323
&prev, fd_table[oldfd]));
321324

325+
out_get_ref:
322326
fs_hnd_ref(fd_table[newfd]);
323327

324328
return newfd;

0 commit comments

Comments
 (0)