From: Christian Schoenebeck <qemu_oss@crudebyte.com>
To: qemu-devel@nongnu.org, Greg Kurz <groug@kaod.org>
Cc: Peter Maydell <peter.maydell@linaro.org>
Subject: Re: [PULL 3/9] 9pfs: local : Introduce local_fid_fd() helper
Date: Fri, 03 Jul 2026 14:49:07 +0200 [thread overview]
Message-ID: <1964729.CQOukoFCf9@weasel> (raw)
In-Reply-To: <CAFEAcA-qtuM51VCzAr+VgsTVkEg1+CsgUNQofVZ0B_zzfodsxA@mail.gmail.com>
On Friday, 3 July 2026 14:21:29 CEST Peter Maydell wrote:
> On Fri, 3 Jul 2026 at 12:41, Christian Schoenebeck
>
> <qemu_oss@crudebyte.com> wrote:
> > On Friday, 3 July 2026 12:52:24 CEST Peter Maydell wrote:
[...]
> > diff --git a/hw/9pfs/9p-local.c b/hw/9pfs/9p-local.c
> > index 4708e170a4..ee592b62f8 100644
> > --- a/hw/9pfs/9p-local.c
> > +++ b/hw/9pfs/9p-local.c
> > @@ -775,8 +775,11 @@ static int local_fid_fd(int fid_type,
> > V9fsFidOpenState *fs)>
> > {
> >
> > if (fid_type == P9_FID_DIR) {
> >
> > return dirfd(fs->dir.stream);
> >
> > - } else {
> > + } else if (fid_type == P9_FID_FILE) {
> >
> > return fs->fd;
> >
> > + } else {
> > + errno = EBADF;
> > + return -1;
> >
> > }
> >
> > }
> >
> > However, that is just a last resort safety net! In 9p.c we are already
> > checking via fid_has_valid_file_handle(), and in the local fs backend via
> > local_has_valid_file_handle(), and in both cases would leave via error
> > path. I have not identified any call path where we would not guard this,
> > so we should never end up passing -1 as FD to a syscall.
> >
> > So this new EBADF / -1 path in local_fid_fd() is just to avoid undefined
> > behaviour as a last resort measure, in case a guard was forgotten in
> > future
> > changes.
>
> That part is fine. What Coverity is warning about is the callsites:
I know, this is just the change that triggered Coverity's warning, because it
made Coverity think that this function might return -1, even though this
branch is actually *never* executed.
> > > > +
> > > >
> > > > static int local_fstat(FsContext *fs_ctx, int fid_type,
> > > >
> > > > V9fsFidOpenState *fs, struct stat *stbuf)
> > > >
> > > > {
> > > >
> > > > - int err, fd;
> > > > -
> > > > - if (fid_type == P9_FID_DIR) {
> > > > - fd = dirfd(fs->dir.stream);
> > > > - } else {
> > > > - fd = fs->fd;
> > > > - }
> > > > + int err, fd = local_fid_fd(fid_type, fs);
> > > >
> > > > err = fstat(fd, stbuf);
> > >
> > > ...but the callsites don't check for error, and instead can
> > > feed -1 into fstat() or other functions, which isn't a valid
> > > filedescriptor.
> >
> > But even then, a syscall receiving -1 for a FD argument would handle this
> > gracefully by returning -1 and errno EBADF.
>
> In practice, yes, but it's not a valid file descriptor. Coverity
> warns about it because it tends to mean "missing error handling",
> as here.
Yes, -1 is defined by POSIX as "invalid file descriptor". And that's the point
why the helper function would return it as last-resort result, because -1 is
well defined by POSIX to not be interchanged with a real file descriptor.
To make this clear: before this change, this helper could potentially return
garbage.
And yes, you can check for every -1 file descriptor just before passing it to
a syscall, even though I already explained that a) you never ever get there
because this check is already made at the higher level call stack, and b) that
every syscall handles -1 as file descriptor gracefully.
If you still think this was not a false positive, you are welcome to send a
patch.
Thank you!
/Christian
next prev parent reply other threads:[~2026-07-03 12:49 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-05-05 9:50 [PULL 0/9] 9p queue 2025-05-05 Christian Schoenebeck
2025-05-05 9:50 ` [PULL 6/9] 9pfs: Introduce futimens file op Christian Schoenebeck
2025-05-05 9:50 ` [PULL 9/9] 9pfs: fix 'total_open_fd' decrementation Christian Schoenebeck
2025-05-05 9:50 ` [PULL 4/9] 9pfs: Don't use file descriptors in core code Christian Schoenebeck
2025-05-05 9:50 ` [PULL 5/9] 9pfs: Introduce ftruncate file op Christian Schoenebeck
2025-05-05 9:50 ` [PULL 2/9] 9pfs: fix FD leak and reduce latency of v9fs_reclaim_fd() Christian Schoenebeck
2025-05-05 9:50 ` [PULL 7/9] tests/9p: add 'Tsetattr' request to test client Christian Schoenebeck
2025-07-10 13:30 ` Peter Maydell
2025-07-10 14:11 ` Christian Schoenebeck
2025-07-10 14:17 ` Peter Maydell
2025-05-05 9:50 ` [PULL 3/9] 9pfs: local : Introduce local_fid_fd() helper Christian Schoenebeck
2026-07-03 10:52 ` Peter Maydell
2026-07-03 11:41 ` Christian Schoenebeck
2026-07-03 12:21 ` Peter Maydell
2026-07-03 12:49 ` Christian Schoenebeck [this message]
2026-07-03 13:15 ` Peter Maydell
2026-07-03 14:47 ` Christian Schoenebeck
2025-05-05 9:50 ` [PULL 8/9] tests/9p: Test `Tsetattr` can truncate unlinked file Christian Schoenebeck
2025-05-05 9:50 ` [PULL 1/9] 9pfs: fix concurrent v9fs_reclaim_fd() calls Christian Schoenebeck
2025-05-06 13:58 ` [PULL 0/9] 9p queue 2025-05-05 Stefan Hajnoczi
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1964729.CQOukoFCf9@weasel \
--to=qemu_oss@crudebyte.com \
--cc=groug@kaod.org \
--cc=peter.maydell@linaro.org \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.