[PATCH v3 13/19] rpc: convert methods using virIdentityPtr to auto free macros

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Reviewed-by: Ján Tomko <jtomko@xxxxxxxxxx>
Signed-off-by: Daniel P. Berrangé <berrange@xxxxxxxxxx>
---
 src/rpc/virnetserverclient.c   | 47 ++++++++++++++--------------------
 src/rpc/virnetserverprogram.c  | 13 +++-------
 tests/virnetserverclienttest.c |  3 +--
 3 files changed, 23 insertions(+), 40 deletions(-)

diff --git a/src/rpc/virnetserverclient.c b/src/rpc/virnetserverclient.c
index 171ee636dd..79287572b6 100644
--- a/src/rpc/virnetserverclient.c
+++ b/src/rpc/virnetserverclient.c
@@ -759,13 +759,13 @@ int virNetServerClientGetUNIXIdentity(virNetServerClientPtr client,
 static virIdentityPtr
 virNetServerClientCreateIdentity(virNetServerClientPtr client)
 {
-    char *username = NULL;
-    char *groupname = NULL;
-    char *seccontext = NULL;
-    virIdentityPtr ret = NULL;
+    g_autofree char *username = NULL;
+    g_autofree char *groupname = NULL;
+    g_autofree char *seccontext = NULL;
+    g_autoptr(virIdentity) ret = NULL;
 
     if (!(ret = virIdentityNew()))
-        goto error;
+        return NULL;
 
     if (client->sock && virNetSocketIsLocal(client->sock)) {
         gid_t gid;
@@ -775,59 +775,50 @@ virNetServerClientCreateIdentity(virNetServerClientPtr client)
         if (virNetSocketGetUNIXIdentity(client->sock,
                                         &uid, &gid, &pid,
                                         &timestamp) < 0)
-            goto error;
+            return NULL;
 
         if (!(username = virGetUserName(uid)))
-            goto error;
+            return NULL;
         if (virIdentitySetUserName(ret, username) < 0)
-            goto error;
+            return NULL;
         if (virIdentitySetUNIXUserID(ret, uid) < 0)
-            goto error;
+            return NULL;
 
         if (!(groupname = virGetGroupName(gid)))
-            goto error;
+            return NULL;
         if (virIdentitySetGroupName(ret, groupname) < 0)
-            goto error;
+            return NULL;
         if (virIdentitySetUNIXGroupID(ret, gid) < 0)
-            goto error;
+            return NULL;
 
         if (virIdentitySetProcessID(ret, pid) < 0)
-            goto error;
+            return NULL;
         if (virIdentitySetProcessTime(ret, timestamp) < 0)
-            goto error;
+            return NULL;
     }
 
 #if WITH_SASL
     if (client->sasl) {
         const char *identity = virNetSASLSessionGetIdentity(client->sasl);
         if (virIdentitySetSASLUserName(ret, identity) < 0)
-            goto error;
+            return NULL;
     }
 #endif
 
     if (client->tls) {
         const char *identity = virNetTLSSessionGetX509DName(client->tls);
         if (virIdentitySetX509DName(ret, identity) < 0)
-            goto error;
+            return NULL;
     }
 
     if (client->sock &&
         virNetSocketGetSELinuxContext(client->sock, &seccontext) < 0)
-        goto error;
+        return NULL;
     if (seccontext &&
         virIdentitySetSELinuxContext(ret, seccontext) < 0)
-        goto error;
-
- cleanup:
-    VIR_FREE(username);
-    VIR_FREE(groupname);
-    VIR_FREE(seccontext);
-    return ret;
+        return NULL;
 
- error:
-    virObjectUnref(ret);
-    ret = NULL;
-    goto cleanup;
+    return g_steal_pointer(&ret);
 }
 
 
diff --git a/src/rpc/virnetserverprogram.c b/src/rpc/virnetserverprogram.c
index 7ae1d2e955..cca820ca5a 100644
--- a/src/rpc/virnetserverprogram.c
+++ b/src/rpc/virnetserverprogram.c
@@ -370,13 +370,13 @@ virNetServerProgramDispatchCall(virNetServerProgramPtr prog,
                                 virNetServerClientPtr client,
                                 virNetMessagePtr msg)
 {
-    char *arg = NULL;
-    char *ret = NULL;
+    g_autofree char *arg = NULL;
+    g_autofree char *ret = NULL;
     int rv = -1;
     virNetServerProgramProcPtr dispatcher;
     virNetMessageError rerr;
     size_t i;
-    virIdentityPtr identity = NULL;
+    g_autoptr(virIdentity) identity = NULL;
 
     memset(&rerr, 0, sizeof(rerr));
 
@@ -484,10 +484,7 @@ virNetServerProgramDispatchCall(virNetServerProgramPtr prog,
     }
 
     xdr_free(dispatcher->ret_filter, ret);
-    VIR_FREE(arg);
-    VIR_FREE(ret);
 
-    virObjectUnref(identity);
     /* Put reply on end of tx queue to send out  */
     return virNetServerClientSendMessage(client, msg);
 
@@ -496,10 +493,6 @@ virNetServerProgramDispatchCall(virNetServerProgramPtr prog,
      * RPC error message we can send back to the client */
     rv = virNetServerProgramSendReplyError(prog, client, msg, &rerr, &msg->header);
 
-    VIR_FREE(arg);
-    VIR_FREE(ret);
-    virObjectUnref(identity);
-
     return rv;
 }
 
diff --git a/tests/virnetserverclienttest.c b/tests/virnetserverclienttest.c
index d094de9840..42393d7dbe 100644
--- a/tests/virnetserverclienttest.c
+++ b/tests/virnetserverclienttest.c
@@ -51,7 +51,7 @@ static int testIdentity(const void *opaque ATTRIBUTE_UNUSED)
     int ret = -1;
     virNetSocketPtr sock = NULL;
     virNetServerClientPtr client = NULL;
-    virIdentityPtr ident = NULL;
+    g_autoptr(virIdentity) ident = NULL;
     const char *gotUsername = NULL;
     uid_t gotUserID;
     const char *gotGroupname = NULL;
@@ -141,7 +141,6 @@ static int testIdentity(const void *opaque ATTRIBUTE_UNUSED)
     if (client)
         virNetServerClientClose(client);
     virObjectUnref(client);
-    virObjectUnref(ident);
     VIR_FORCE_CLOSE(sv[0]);
     VIR_FORCE_CLOSE(sv[1]);
     return ret;
-- 
2.21.0

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux