[PATCH 4/4] doRemoteOpen: Refactor control flow

Peter Krempa pkrempa at redhat.com
Wed May 18 09:22:13 UTC 2022


Use a temporary variable 'newconn' to hold the newly opened connection
until we are ready to pass it back instead of the original connection.

This way we can avoid complicated 'error'/'cleanup' sections.

Signed-off-by: Peter Krempa <pkrempa at redhat.com>
---
 src/remote/remote_daemon_dispatch.c | 28 ++++++++++++----------------
 1 file changed, 12 insertions(+), 16 deletions(-)

diff --git a/src/remote/remote_daemon_dispatch.c b/src/remote/remote_daemon_dispatch.c
index 39953f46cf..c1f85925a3 100644
--- a/src/remote/remote_daemon_dispatch.c
+++ b/src/remote/remote_daemon_dispatch.c
@@ -1794,7 +1794,7 @@ remoteOpenConn(const char *uri,
                virConnectPtr *conn)
 {
     g_autoptr(virTypedParamList) identparams = NULL;
-    int ret = -1;
+    g_autoptr(virConnect) newconn = NULL;

     VIR_DEBUG("Getting secondary uri=%s readonly=%d preserveIdent=%d conn=%p",
               NULLSTR(uri), readonly, preserveIdentity, conn);
@@ -1814,34 +1814,30 @@ remoteOpenConn(const char *uri,
             return -1;

         if (!(identparams = virIdentityGetParameters(ident)))
-            goto error;
+            return -1;
     }

     VIR_DEBUG("Opening driver %s", uri);
     if (readonly)
-        *conn = virConnectOpenReadOnly(uri);
+        newconn = virConnectOpenReadOnly(uri);
     else
-        *conn = virConnectOpen(uri);
-    if (!*conn)
-        goto error;
-    VIR_DEBUG("Opened driver %p", *conn);
+        newconn = virConnectOpen(uri);
+
+    if (!newconn)
+        return -1;
+
+    VIR_DEBUG("Opened driver %p", newconn);

     if (preserveIdentity) {
         if (virConnectSetIdentity(*conn, identparams->par, identparams->npar, 0) < 0)
-            goto error;
+            return -1;

         VIR_DEBUG("Forwarded current identity to secondary driver");
     }

-    ret = 0;
- cleanup:
-    return ret;
+    *conn = g_steal_pointer(&newconn);

- error:
-    if (*conn) {
-        g_clear_pointer(conn, virConnectClose);
-    }
-    goto cleanup;
+    return 0;
 }


-- 
2.35.3



More information about the libvir-list mailing list