This is an automated email from the ASF dual-hosted git repository.

acassis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 0dbef84ccb4ddbb4bd355f01174cda8ad9afd1a5
Author: Marco Casaroli <[email protected]>
AuthorDate: Mon Nov 2 08:29:51 2026 +0100

    fs/vfs: Return EBADF for a read or write in the wrong access mode.
    
    read(), readv() and pread() on a descriptor that is open only for
    writing, and write(), writev() and pwrite() on a descriptor that is open
    only for reading, failed with EACCES.  POSIX specifies EBADF for both:
    "The fildes argument is not a valid file descriptor open for reading"
    (read) and "... open for writing" (write).  The write() and writev()
    comments in fs_write.c already document EBADF, and aio_read() documents
    EBADF for a descriptor that is not open for reading.
    
    Change the access mode checks in file_read(), file_readv(),
    file_write() and file_writev() to return -EBADF.  This covers the pipe
    ends, pread(), pwrite(), sendfile() and the aio worker, which all go
    through these functions.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 fs/vfs/fs_read.c  | 4 ++--
 fs/vfs/fs_write.c | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/fs/vfs/fs_read.c b/fs/vfs/fs_read.c
index 9be340d9ba5..2384365c11a 100644
--- a/fs/vfs/fs_read.c
+++ b/fs/vfs/fs_read.c
@@ -204,7 +204,7 @@ ssize_t file_readv(FAR struct file *filep,
     {
       /* No.. File is not read-able */
 
-      ret = -EACCES;
+      ret = -EBADF;
     }
 
   /* Is a driver or mountpoint registered? If so, does it support the read
@@ -303,7 +303,7 @@ ssize_t file_read(FAR struct file *filep, FAR void *buf, 
size_t nbytes)
     {
       /* No.. File is not read-able */
 
-      return -EACCES;
+      return -EBADF;
     }
 
   if (nbytes == 0)
diff --git a/fs/vfs/fs_write.c b/fs/vfs/fs_write.c
index 2a225bb86a6..c290c868d2a 100644
--- a/fs/vfs/fs_write.c
+++ b/fs/vfs/fs_write.c
@@ -156,7 +156,7 @@ ssize_t file_writev(FAR struct file *filep,
 
   if ((filep->f_oflags & O_ACCMODE) == O_RDONLY)
     {
-      return -EACCES;
+      return -EBADF;
     }
 
   /* Check buffer count and pointer for iovec */
@@ -278,7 +278,7 @@ ssize_t file_write(FAR struct file *filep, FAR const void 
*buf,
     {
       /* No.. File is not writable */
 
-      return -EACCES;
+      return -EBADF;
     }
 
   if (nbytes == 0)

Reply via email to