From 5e2059c284bb851a8520ada426788fc216578938 Mon Sep 17 00:00:00 2001 From: Matheus Date: Tue, 20 Sep 2022 17:12:54 -0300 Subject: [PATCH] fd: bugfixes - add an extra check - properly set the fd entry in the table to 0 in a refcount > 0 free - only close the fd node if there's an open node --- src/fs/fd.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/fs/fd.c b/src/fs/fd.c index 4428a45f..fbea0aee 100644 --- a/src/fs/fd.c +++ b/src/fs/fd.c @@ -13,7 +13,7 @@ int fd_release(fd_t* fd){ int fd_access(fdtable_t* fdtable, fd_t** fd, int ifd){ spinlock_acquire(&fdtable->lock); - if(ifd >= fdtable->fdcount || fdtable->fd[ifd] == NULL){ + if(ifd >= fdtable->fdcount || fdtable->fd[ifd] == NULL || fdtable->fd[ifd]->node == NULL){ spinlock_release(&fdtable->lock); return EBADF; } @@ -91,8 +91,9 @@ int fd_free(fdtable_t* fdtable, int ifd){ fd_t* fd = fdtable->fd[ifd]; spinlock_acquire(&fd->lock); - + if(--fd->refcount > 0){ + fdtable->fd[ifd] = NULL; spinlock_release(&fd->lock); spinlock_release(&fdtable->lock); return 0; @@ -101,9 +102,10 @@ int fd_free(fdtable_t* fdtable, int ifd){ fdtable->fd[ifd] = NULL; spinlock_release(&fdtable->lock); + int err = 0; + if(fd->node) + err = vfs_close(fd->node); - int err = vfs_close(fd->node); - free(fd); return err;