qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Eric Blake <eblake@redhat.com>
To: Paolo Bonzini <pbonzini@redhat.com>, qemu-devel@nongnu.org
Cc: qemu-trivial@nongnu.org
Subject: Re: [Qemu-devel] [PATCH] pci-assign: avoid pointless stat
Date: Wed, 4 Jan 2017 11:05:51 -0600	[thread overview]
Message-ID: <dfd6fda1-a6a1-8001-2e69-6890ab7948c5@redhat.com> (raw)
In-Reply-To: <20170104150525.4246-1-pbonzini@redhat.com>

[-- Attachment #1: Type: text/plain, Size: 1758 bytes --]

On 01/04/2017 09:05 AM, Paolo Bonzini wrote:
> Just check the errno value after fopen and follow it with fstat.
> This shuts up Coverity's complaint about TOC/TOU violation.
> 
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> ---
>  hw/i386/pci-assign-load-rom.c | 16 ++++++++--------
>  1 file changed, 8 insertions(+), 8 deletions(-)

Reviewed-by: Eric Blake <eblake@redhat.com>

> 
> diff --git a/hw/i386/pci-assign-load-rom.c b/hw/i386/pci-assign-load-rom.c
> index 0d8e4b2..fd59076 100644
> --- a/hw/i386/pci-assign-load-rom.c
> +++ b/hw/i386/pci-assign-load-rom.c
> @@ -39,19 +39,19 @@ void *pci_assign_dev_load_option_rom(PCIDevice *dev, struct Object *owner,
>               "/sys/bus/pci/devices/%04x:%02x:%02x.%01x/rom",
>               domain, bus, slot, function);
>  
> -    if (stat(rom_file, &st)) {
> -        if (errno != ENOENT) {
> -            error_report("pci-assign: Invalid ROM.");
> -        }
> -        return NULL;
> -    }
> -
>      /* Write "1" to the ROM file to enable it */
>      fp = fopen(rom_file, "r+");
>      if (fp == NULL) {
> -        error_report("pci-assign: Cannot open %s: %s", rom_file, strerror(errno));
> +        if (errno != ENOENT) {
> +            error_report("pci-assign: Cannot open %s: %s", rom_file, strerror(errno));
> +        }
>          return NULL;
>      }
> +    if (fstat(fileno(fp), &st) == -1) {
> +        error_report("pci-assign: Cannot stat %s: %s", rom_file, strerror(errno));
> +        goto close_rom;

fstat() failure after fopen() success is quite unlikely, but I suppose
it is still possible in some weird situations.

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 604 bytes --]

  reply	other threads:[~2017-01-04 17:05 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-01-04 15:05 [Qemu-devel] [PATCH] pci-assign: avoid pointless stat Paolo Bonzini
2017-01-04 17:05 ` Eric Blake [this message]
2017-01-12 10:54 ` Michael Tokarev

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=dfd6fda1-a6a1-8001-2e69-6890ab7948c5@redhat.com \
    --to=eblake@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-trivial@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).