]> xenbits.xensource.com Git - libvirt.git/commitdiff
virpci: Resolve coverity issues
authorJohn Ferlan <jferlan@redhat.com>
Thu, 6 Feb 2014 17:30:57 +0000 (12:30 -0500)
committerJohn Ferlan <jferlan@redhat.com>
Fri, 7 Feb 2014 15:58:24 +0000 (10:58 -0500)
Coverity complains about "USE_AFTER_FREE" due to how virPCIDeviceSetStubDriver
"could" return either -1, 0, or 1 from the VIR_STRDUP() and then possibly makes
a call to virPCIDeviceDetach().

The only way this could happen is if NULL were passed as the "driver" name
and virStrdup() returned 0.  Since the calling functions check < 0 on the
initial function call, the 0 possibility causes Coverity to complain.

To fix this - enforce that the second parameter is not NULL using
ATTRIBUTE_NONNULL(2) for the function prototype, then in virPCIDeviceDetach
add an sa_assert(dev->stubDriver). This will result in Coverity not complaining
any more.

src/util/virpci.c
src/util/virpci.h

index c3d211f64b0295ceb279c6a423ce7dea31a36a3f..00d106441bd4959b007e5b19a212768b4a65723e 100644 (file)
@@ -1327,6 +1327,8 @@ virPCIDeviceDetach(virPCIDevicePtr dev,
                    virPCIDeviceList *activeDevs,
                    virPCIDeviceList *inactiveDevs)
 {
+    sa_assert(dev->stubDriver);
+
     if (virPCIProbeStubDriver(dev->stubDriver) < 0)
         return -1;
 
@@ -1657,7 +1659,7 @@ int
 virPCIDeviceSetStubDriver(virPCIDevicePtr dev, const char *driver)
 {
     VIR_FREE(dev->stubDriver);
-    return driver ? VIR_STRDUP(dev->stubDriver, driver) : 0;
+    return VIR_STRDUP(dev->stubDriver, driver);
 }
 
 const char *
index 42c3c957e2376193ef628c1931a91a078e58baf6..ac6dae18e07a9ea4e492d918fe2f31496663fe06 100644 (file)
@@ -63,7 +63,8 @@ void virPCIDeviceSetManaged(virPCIDevice *dev,
                             bool managed);
 unsigned int virPCIDeviceGetManaged(virPCIDevice *dev);
 int virPCIDeviceSetStubDriver(virPCIDevicePtr dev,
-                              const char *driver);
+                              const char *driver)
+    ATTRIBUTE_NONNULL(2);
 const char *virPCIDeviceGetStubDriver(virPCIDevicePtr dev);
 void virPCIDeviceSetUsedBy(virPCIDevice *dev,
                            const char *used_by);