Skip to contentSkip to conversation

pci-assign: avoid pointless stat

PATCH Applied Paolo Bonzini 3 participants 3 messages Jan 4, 2017 – Jan 12, 2017

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 pci-assign: avoid pointless stat

Just check the errno value after fopen and follow it with fstat. This shuts up Coverity's complaint about TOC/TOU violation.

---
 hw/i386/pci-assign-load-rom.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
hw/i386/pci-assign-load-rom.c +8 -8
index 0d8e4b2..fd59076 100644
@@ -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;
+    }
+
     val = 1;
     if (fwrite(&val, 1, 1, fp) != 1) {
         goto close_rom;
Signature · 1 line
2.9.3
Reviewed-by on v1 1/1
#1 lore raw
Quoted 7 lines · Paolo Bonzini

Just check the errno value after fopen and follow it with fstat. This shuts up Coverity's complaint about TOC/TOU violation.

---
 hw/i386/pci-assign-load-rom.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
Quoted 18 lines
hw/i386/pci-assign-load-rom.c +0 -7
index 0d8e4b2..fd59076 100644
@@ -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+");

hw/i386/pci-assign-load-rom.c void *pci_assign_dev_load_option_rom(PCIDevice *dev, struct Object *owner, · line 52

Quoted 10 lines
hw/i386/pci-assign-load-rom.c +6 -1
     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.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#2 lore raw
In reply to Paolo Bonzini
Quoted 2 lines · Paolo Bonzini

Just check the errno value after fopen and follow it with fstat. This shuts up Coverity's complaint about TOC/TOU violation.

Applied to -trivial, thanks!

/mjt