[PATCH 4/9] util: simplify virPCIProbeStubDriver()

Laine Stump laine at redhat.com
Wed Oct 21 02:26:04 UTC 2020


This function had a loop that was only executed twice; it was
artificially constructed with a label, a goto, and a boolean to tell
that it had already been executed once. Aside from that, the body of
the loop contained only two lines that needed to be repeated (the
second time through, everything beyond those two lines would be
skipped).

One side effect of this strange loop was that a g_autofree string was
manually freed and re-initialized; I've been told that manually
freeing a g_auto_free object is highly discouraged.

This patch refactors the function to simply repeat the 2 lines that
might possibly be executed twice, thus eliminating the ugly use of
goto to construct a loop, and also takes advantage of the fact that
virPCIDriverDir() was previously returning *exactly* the same string
both times it was called to eliminate the manual VIR_FREE of drvpath.

Signed-off-by: Laine Stump <laine at redhat.com>
---
 src/util/virpci.c | 22 +++++++++-------------
 1 file changed, 9 insertions(+), 13 deletions(-)

diff --git a/src/util/virpci.c b/src/util/virpci.c
index ac37a60576..045121c453 100644
--- a/src/util/virpci.c
+++ b/src/util/virpci.c
@@ -988,7 +988,7 @@ virPCIProbeStubDriver(virPCIStubDriver driver)
 {
     const char *drvname = NULL;
     g_autofree char *drvpath = NULL;
-    bool probed = false;
+    g_autofree char *errbuf = NULL;
 
     if (driver == VIR_PCI_STUB_DRIVER_NONE ||
         !(drvname = virPCIStubDriverTypeToString(driver))) {
@@ -998,25 +998,21 @@ virPCIProbeStubDriver(virPCIStubDriver driver)
         return -1;
     }
 
- recheck:
     drvpath = virPCIDriverDir(drvname);
 
+    /* driver previously loaded, return */
     if (virFileExists(drvpath))
-        /* driver already loaded, return */
         return 0;
 
-    if (!probed) {
-        g_autofree char *errbuf = NULL;
-        probed = true;
-        if ((errbuf = virKModLoad(drvname))) {
-            VIR_WARN("failed to load driver %s: %s", drvname, errbuf);
-            goto cleanup;
-        }
-
-        VIR_FREE(drvpath);
-        goto recheck;
+    if ((errbuf = virKModLoad(drvname))) {
+        VIR_WARN("failed to load driver %s: %s", drvname, errbuf);
+        goto cleanup;
     }
 
+    /* driver loaded after probing */
+    if (virFileExists(drvpath))
+        return 0;
+
  cleanup:
     /* If we know failure was because of admin config, let's report that;
      * otherwise, report a more generic failure message
-- 
2.26.2




More information about the libvir-list mailing list