[libvirt] [PATCH 24/34] rpc: use g_strdup instead of VIR_STRDUP

Ján Tomko jtomko at redhat.com
Sun Oct 20 12:55:42 UTC 2019


Replace all occurrences of
  if (VIR_STRDUP(a, b) < 0)
     /* effectively dead code */
with:
  a = g_strdup(b);

Signed-off-by: Ján Tomko <jtomko at redhat.com>
---
 src/rpc/gendispatch.pl        | 20 ++++++--------------
 src/rpc/virnetclient.c        | 15 +++++----------
 src/rpc/virnetlibsshsession.c | 19 ++++++-------------
 src/rpc/virnetmessage.c       | 26 ++++++++++----------------
 src/rpc/virnetserver.c        |  3 +--
 src/rpc/virnetserverclient.c  |  3 +--
 src/rpc/virnetsocket.c        |  9 +++------
 src/rpc/virnetsshsession.c    | 30 ++++++++++--------------------
 src/rpc/virnettlscontext.c    | 21 +++++++--------------
 9 files changed, 49 insertions(+), 97 deletions(-)

diff --git a/src/rpc/gendispatch.pl b/src/rpc/gendispatch.pl
index 6d9838c398..fab4513fd1 100755
--- a/src/rpc/gendispatch.pl
+++ b/src/rpc/gendispatch.pl
@@ -773,10 +773,9 @@ elsif ($mode eq "server") {
                         # SPECIAL: virConnectGetType returns a constant string that must
                         #          not be freed. Therefore, duplicate the string here.
                         push(@vars_list, "const char *$1");
-                        push(@ret_list, "/* We have to VIR_STRDUP because remoteDispatchClientRequest will");
+                        push(@ret_list, "/* We have to g_strdup because remoteDispatchClientRequest will");
                         push(@ret_list, " * free this string after it's been serialised. */");
-                        push(@ret_list, "if (VIR_STRDUP(ret->type, type) < 0)");
-                        push(@ret_list, "    goto cleanup;");
+                        push(@ret_list, "ret->type = g_strdup(type);");
                     } else {
                         push(@vars_list, "char *$1");
                         push(@ret_list, "ret->$1 = $1;");
@@ -795,8 +794,7 @@ elsif ($mode eq "server") {
                          "if (VIR_ALLOC($1_p) < 0)\n" .
                          "        goto cleanup;\n" .
                          "\n" .
-                         "    if (VIR_STRDUP(*$1_p, $1) < 0)\n".
-                         "        goto cleanup;\n");
+                         "    *$1_p = g_strdup($1);\n");
 
                     $single_ret_var = $1;
                     $single_ret_by_ref = 0;
@@ -1936,17 +1934,11 @@ elsif ($mode eq "client") {
         if ($single_ret_as_list) {
             print "    /* This call is caller-frees (although that isn't clear from\n";
             print "     * the documentation).  However xdr_free will free up both the\n";
-            print "     * names and the list of pointers, so we have to VIR_STRDUP the\n";
+            print "     * names and the list of pointers, so we have to g_strdup the\n";
             print "     * names here. */\n";
             print "    for (i = 0; i < ret.$single_ret_list_name.${single_ret_list_name}_len; ++i) {\n";
-            print "        if (VIR_STRDUP(${single_ret_list_name}[i],\n";
-            print "                       ret.$single_ret_list_name.${single_ret_list_name}_val[i]) < 0) {\n";
-            print "            size_t j;\n";
-            print "            for (j = 0; j < i; j++)\n";
-            print "                VIR_FREE(${single_ret_list_name}[j]);\n";
-            print "\n";
-            print "            goto cleanup;\n";
-            print "        }\n";
+            print "        ${single_ret_list_name}[i] = \n";
+            print "            g_strdup(ret.$single_ret_list_name.${single_ret_list_name}_val[i]);\n";
             print "    }\n";
             print "\n";
         } elsif ($modern_ret_as_list) {
diff --git a/src/rpc/virnetclient.c b/src/rpc/virnetclient.c
index 1d846cd131..678e6f7815 100644
--- a/src/rpc/virnetclient.c
+++ b/src/rpc/virnetclient.c
@@ -316,8 +316,7 @@ static virNetClientPtr virNetClientNew(virNetSocketPtr sock,
     client->wakeupSendFD = wakeupFD[1];
     wakeupFD[0] = wakeupFD[1] = -1;
 
-    if (VIR_STRDUP(client->hostname, hostname) < 0)
-        goto error;
+    client->hostname = g_strdup(hostname);
 
     PROBE(RPC_CLIENT_NEW,
           "client=%p sock=%p",
@@ -454,8 +453,7 @@ virNetClientPtr virNetClientNewLibSSH2(const char *host,
 
     /* Use default paths for known hosts an public keys if not provided */
     if (knownHostsPath) {
-        if (VIR_STRDUP(knownhosts, knownHostsPath) < 0)
-            goto cleanup;
+        knownhosts = g_strdup(knownHostsPath);
     } else {
         confdir = virGetUserConfigDirectory();
         if (confdir) {
@@ -466,8 +464,7 @@ virNetClientPtr virNetClientNewLibSSH2(const char *host,
     }
 
     if (privkeyPath) {
-        if (VIR_STRDUP(privkey, privkeyPath) < 0)
-            goto cleanup;
+        privkey = g_strdup(privkeyPath);
     } else {
         homedir = virGetUserDirectory();
         if (homedir) {
@@ -565,8 +562,7 @@ virNetClientPtr virNetClientNewLibssh(const char *host,
 
     /* Use default paths for known hosts an public keys if not provided */
     if (knownHostsPath) {
-        if (VIR_STRDUP(knownhosts, knownHostsPath) < 0)
-            goto cleanup;
+        knownhosts = g_strdup(knownHostsPath);
     } else {
         confdir = virGetUserConfigDirectory();
         if (confdir) {
@@ -576,8 +572,7 @@ virNetClientPtr virNetClientNewLibssh(const char *host,
     }
 
     if (privkeyPath) {
-        if (VIR_STRDUP(privkey, privkeyPath) < 0)
-            goto cleanup;
+        privkey = g_strdup(privkeyPath);
     } else {
         homedir = virGetUserDirectory();
         if (homedir) {
diff --git a/src/rpc/virnetlibsshsession.c b/src/rpc/virnetlibsshsession.c
index cd92329230..0b98236f38 100644
--- a/src/rpc/virnetlibsshsession.c
+++ b/src/rpc/virnetlibsshsession.c
@@ -1052,11 +1052,8 @@ virNetLibsshSessionAuthAddPrivKeyAuth(virNetLibsshSessionPtr sess,
 
     virObjectLock(sess);
 
-    if (VIR_STRDUP(file, keyfile) < 0 ||
-        VIR_STRDUP(pass, password) < 0) {
-        ret = -1;
-        goto error;
-    }
+    file = g_strdup(keyfile);
+    pass = g_strdup(password);
 
     if (!(auth = virNetLibsshSessionAuthMethodNew(sess))) {
         ret = -1;
@@ -1114,8 +1111,7 @@ virNetLibsshSessionSetChannelCommand(virNetLibsshSessionPtr sess,
 
     VIR_FREE(sess->channelCommand);
 
-    if (VIR_STRDUP(sess->channelCommand, command) < 0)
-        ret = -1;
+    sess->channelCommand = g_strdup(command);
 
     virObjectUnlock(sess);
     return ret;
@@ -1135,8 +1131,7 @@ virNetLibsshSessionSetHostKeyVerification(virNetLibsshSessionPtr sess,
 
     VIR_FREE(sess->hostname);
 
-    if (VIR_STRDUP(sess->hostname, hostname) < 0)
-        goto error;
+    sess->hostname = g_strdup(hostname);
 
     /* set the hostname */
     if (ssh_options_set(sess->session, SSH_OPTIONS_HOST, sess->hostname) < 0)
@@ -1156,8 +1151,7 @@ virNetLibsshSessionSetHostKeyVerification(virNetLibsshSessionPtr sess,
             goto error;
 
         VIR_FREE(sess->knownHostsFile);
-        if (VIR_STRDUP(sess->knownHostsFile, hostsfile) < 0)
-            goto error;
+        sess->knownHostsFile = g_strdup(hostsfile);
     } else {
         /* libssh does not support trying no known_host file at all:
          * hence use /dev/null here, without storing it as file */
@@ -1191,8 +1185,7 @@ virNetLibsshSessionPtr virNetLibsshSessionNew(const char *username)
         goto error;
     }
 
-    if (VIR_STRDUP(sess->username, username) < 0)
-        goto error;
+    sess->username = g_strdup(username);
 
     VIR_DEBUG("virNetLibsshSessionPtr: %p, ssh_session: %p",
               sess, sess->session);
diff --git a/src/rpc/virnetmessage.c b/src/rpc/virnetmessage.c
index bb6ef9a9ae..9d2a8a6d7d 100644
--- a/src/rpc/virnetmessage.c
+++ b/src/rpc/virnetmessage.c
@@ -523,28 +523,22 @@ void virNetMessageSaveError(virNetMessageErrorPtr rerr)
     if (verr) {
         rerr->code = verr->code;
         rerr->domain = verr->domain;
-        if (verr->message && VIR_ALLOC(rerr->message) == 0 &&
-            VIR_STRDUP_QUIET(*rerr->message, verr->message) < 0)
-            VIR_FREE(rerr->message);
+        if (verr->message && VIR_ALLOC(rerr->message) == 0)
+            *rerr->message = g_strdup(verr->message);
         rerr->level = verr->level;
-        if (verr->str1 && VIR_ALLOC(rerr->str1) == 0 &&
-            VIR_STRDUP_QUIET(*rerr->str1, verr->str1) < 0)
-            VIR_FREE(rerr->str1);
-        if (verr->str2 && VIR_ALLOC(rerr->str2) == 0 &&
-            VIR_STRDUP_QUIET(*rerr->str2, verr->str2) < 0)
-            VIR_FREE(rerr->str2);
-        if (verr->str3 && VIR_ALLOC(rerr->str3) == 0 &&
-            VIR_STRDUP_QUIET(*rerr->str3, verr->str3) < 0)
-            VIR_FREE(rerr->str3);
+        if (verr->str1 && VIR_ALLOC(rerr->str1) == 0)
+            *rerr->str1 = g_strdup(verr->str1);
+        if (verr->str2 && VIR_ALLOC(rerr->str2) == 0)
+            *rerr->str2 = g_strdup(verr->str2);
+        if (verr->str3 && VIR_ALLOC(rerr->str3) == 0)
+            *rerr->str3 = g_strdup(verr->str3);
         rerr->int1 = verr->int1;
         rerr->int2 = verr->int2;
     } else {
         rerr->code = VIR_ERR_INTERNAL_ERROR;
         rerr->domain = VIR_FROM_RPC;
-        if (VIR_ALLOC_QUIET(rerr->message) == 0 &&
-            VIR_STRDUP_QUIET(*rerr->message,
-                             _("Library function returned error but did not set virError")) < 0)
-            VIR_FREE(rerr->message);
+        if (VIR_ALLOC_QUIET(rerr->message) == 0)
+            *rerr->message = g_strdup(_("Library function returned error but did not set virError"));
         rerr->level = VIR_ERR_ERROR;
     }
 }
diff --git a/src/rpc/virnetserver.c b/src/rpc/virnetserver.c
index e229f57bab..042661ffa5 100644
--- a/src/rpc/virnetserver.c
+++ b/src/rpc/virnetserver.c
@@ -365,8 +365,7 @@ virNetServerPtr virNetServerNew(const char *name,
                                           srv)))
         goto error;
 
-    if (VIR_STRDUP(srv->name, name) < 0)
-        goto error;
+    srv->name = g_strdup(name);
 
     srv->next_client_id = next_client_id;
     srv->nclients_max = max_clients;
diff --git a/src/rpc/virnetserverclient.c b/src/rpc/virnetserverclient.c
index 8482c5c29c..67b3bf9531 100644
--- a/src/rpc/virnetserverclient.c
+++ b/src/rpc/virnetserverclient.c
@@ -1665,8 +1665,7 @@ virNetServerClientGetInfo(virNetServerClientPtr client,
         goto cleanup;
     }
 
-    if (VIR_STRDUP(*sock_addr, addr) < 0)
-        goto cleanup;
+    *sock_addr = g_strdup(addr);
 
     if (!client->identity) {
         virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
diff --git a/src/rpc/virnetsocket.c b/src/rpc/virnetsocket.c
index 15791ceb35..6574ee0d16 100644
--- a/src/rpc/virnetsocket.c
+++ b/src/rpc/virnetsocket.c
@@ -999,8 +999,7 @@ virNetSocketNewConnectLibSSH2(const char *host,
     if (virNetSSHSessionSetChannelCommand(sess, command) != 0)
         goto error;
 
-    if (VIR_STRDUP(authMethodsCopy, authMethods) < 0)
-        goto error;
+    authMethodsCopy = g_strdup(authMethods);
 
     authMethodNext = authMethodsCopy;
 
@@ -1134,8 +1133,7 @@ virNetSocketNewConnectLibssh(const char *host,
     if (virNetLibsshSessionSetChannelCommand(sess, command) != 0)
         goto error;
 
-    if (VIR_STRDUP(authMethodsCopy, authMethods) < 0)
-        goto error;
+    authMethodsCopy = g_strdup(authMethods);
 
     authMethodNext = authMethodsCopy;
 
@@ -1635,8 +1633,7 @@ int virNetSocketGetSELinuxContext(virNetSocketPtr sock,
         goto cleanup;
     }
 
-    if (VIR_STRDUP(*context, seccon) < 0)
-        goto cleanup;
+    *context = g_strdup(seccon);
 
     ret = 0;
  cleanup:
diff --git a/src/rpc/virnetsshsession.c b/src/rpc/virnetsshsession.c
index de39ba31dc..931c7091cc 100644
--- a/src/rpc/virnetsshsession.c
+++ b/src/rpc/virnetsshsession.c
@@ -232,10 +232,7 @@ virNetSSHKbIntCb(const char *name G_GNUC_UNUSED,
     /* fill data structures for auth callback */
     for (i = 0; i < num_prompts; i++) {
         char *prompt;
-        if (VIR_STRDUP(prompt, prompts[i].text) < 0) {
-            priv->authCbErr = VIR_NET_SSH_AUTHCB_OOM;
-            goto cleanup;
-        }
+        prompt = g_strdup(prompts[i].text);
         askcred[i].prompt = prompt;
 
         /* remove colon and trailing spaces from prompts, as default behavior
@@ -1018,8 +1015,7 @@ virNetSSHSessionAuthAddPasswordAuth(virNetSSHSessionPtr sess,
                                             "ssh", NULL, sess->hostname)))
             goto error;
     } else {
-        if (VIR_STRDUP(user, username) < 0)
-            goto error;
+        user = g_strdup(username);
     }
 
     virObjectLock(sess);
@@ -1055,8 +1051,7 @@ virNetSSHSessionAuthAddAgentAuth(virNetSSHSessionPtr sess,
 
     virObjectLock(sess);
 
-    if (VIR_STRDUP(user, username) < 0)
-        goto error;
+    user = g_strdup(username);
 
     if (!(auth = virNetSSHSessionAuthMethodNew(sess)))
         goto error;
@@ -1094,10 +1089,9 @@ virNetSSHSessionAuthAddPrivKeyAuth(virNetSSHSessionPtr sess,
 
     virObjectLock(sess);
 
-    if (VIR_STRDUP(user, username) < 0 ||
-        VIR_STRDUP(file, keyfile) < 0 ||
-        VIR_STRDUP(pass, password) < 0)
-        goto error;
+    user = g_strdup(username);
+    file = g_strdup(keyfile);
+    pass = g_strdup(password);
 
     if (!(auth = virNetSSHSessionAuthMethodNew(sess)))
         goto error;
@@ -1135,8 +1129,7 @@ virNetSSHSessionAuthAddKeyboardAuth(virNetSSHSessionPtr sess,
 
     virObjectLock(sess);
 
-    if (VIR_STRDUP(user, username) < 0)
-        goto error;
+    user = g_strdup(username);
 
     if (!(auth = virNetSSHSessionAuthMethodNew(sess)))
         goto error;
@@ -1164,8 +1157,7 @@ virNetSSHSessionSetChannelCommand(virNetSSHSessionPtr sess,
 
     VIR_FREE(sess->channelCommand);
 
-    if (VIR_STRDUP(sess->channelCommand, command) < 0)
-        ret = -1;
+    sess->channelCommand = g_strdup(command);
 
     virObjectUnlock(sess);
     return ret;
@@ -1188,8 +1180,7 @@ virNetSSHSessionSetHostKeyVerification(virNetSSHSessionPtr sess,
 
     VIR_FREE(sess->hostname);
 
-    if (VIR_STRDUP(sess->hostname, hostname) < 0)
-        goto error;
+    sess->hostname = g_strdup(hostname);
 
     /* load the known hosts file */
     if (hostsfile) {
@@ -1213,8 +1204,7 @@ virNetSSHSessionSetHostKeyVerification(virNetSSHSessionPtr sess,
         /* set filename only if writing to the known hosts file is requested */
         if (!(flags & VIR_NET_SSH_HOSTKEY_FILE_READONLY)) {
             VIR_FREE(sess->knownHostsFile);
-            if (VIR_STRDUP(sess->knownHostsFile, hostsfile) < 0)
-                goto error;
+            sess->knownHostsFile = g_strdup(hostsfile);
         }
     }
 
diff --git a/src/rpc/virnettlscontext.c b/src/rpc/virnettlscontext.c
index e4fbe6e59a..2420ad8681 100644
--- a/src/rpc/virnettlscontext.c
+++ b/src/rpc/virnettlscontext.c
@@ -702,8 +702,7 @@ static virNetTLSContextPtr virNetTLSContextNew(const char *cacert,
     if (!(ctxt = virObjectLockableNew(virNetTLSContextClass)))
         return NULL;
 
-    if (VIR_STRDUP(ctxt->priority, priority) < 0)
-        goto error;
+    ctxt->priority = g_strdup(priority);
 
     err = gnutls_certificate_allocate_credentials(&ctxt->x509cred);
     if (err) {
@@ -863,23 +862,19 @@ static int virNetTLSContextLocateCredentials(const char *pkipath,
      */
     if (!*cacert) {
         VIR_DEBUG("Using default TLS CA certificate path");
-        if (VIR_STRDUP(*cacert, LIBVIRT_CACERT) < 0)
-            goto error;
+        *cacert = g_strdup(LIBVIRT_CACERT);
     }
 
     if (!*cacrl) {
         VIR_DEBUG("Using default TLS CA revocation list path");
-        if (VIR_STRDUP(*cacrl, LIBVIRT_CACRL) < 0)
-            goto error;
+        *cacrl = g_strdup(LIBVIRT_CACRL);
     }
 
     if (!*key && !*cert) {
         VIR_DEBUG("Using default TLS key/certificate path");
-        if (VIR_STRDUP(*key, isServer ? LIBVIRT_SERVERKEY : LIBVIRT_CLIENTKEY) < 0)
-            goto error;
+        *key = g_strdup(isServer ? LIBVIRT_SERVERKEY : LIBVIRT_CLIENTKEY);
 
-        if (VIR_STRDUP(*cert, isServer ? LIBVIRT_SERVERCERT : LIBVIRT_CLIENTCERT) < 0)
-            goto error;
+        *cert = g_strdup(isServer ? LIBVIRT_SERVERCERT : LIBVIRT_CLIENTCERT);
     }
 
     VIR_FREE(user_pki_path);
@@ -1058,8 +1053,7 @@ static int virNetTLSContextValidCertificate(virNetTLSContextPtr ctxt,
                                "[session]", gnutls_strerror(ret));
                 goto authfail;
             }
-            if (VIR_STRDUP(sess->x509dname, dname) < 0)
-                goto authfail;
+            sess->x509dname = g_strdup(dname);
             VIR_DEBUG("Peer DN is %s", dname);
 
             if (virNetTLSContextCheckCertDN(cert, "[session]", sess->hostname, dname,
@@ -1195,8 +1189,7 @@ virNetTLSSessionPtr virNetTLSSessionNew(virNetTLSContextPtr ctxt,
     if (!(sess = virObjectLockableNew(virNetTLSSessionClass)))
         return NULL;
 
-    if (VIR_STRDUP(sess->hostname, hostname) < 0)
-        goto error;
+    sess->hostname = g_strdup(hostname);
 
     if ((err = gnutls_init(&sess->session,
                            ctxt->isServer ? GNUTLS_SERVER : GNUTLS_CLIENT)) != 0) {
-- 
2.21.0




More information about the libvir-list mailing list