]> xenbits.xensource.com Git - libvirt.git/commitdiff
rpc: Use g_strdup_printf() instead of virAsprintf()
authorMichal Privoznik <mprivozn@redhat.com>
Tue, 22 Oct 2019 13:26:14 +0000 (15:26 +0200)
committerMichal Privoznik <mprivozn@redhat.com>
Tue, 12 Nov 2019 15:15:58 +0000 (16:15 +0100)
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com>
src/rpc/virnetclient.c
src/rpc/virnetlibsshsession.c
src/rpc/virnetsocket.c
src/rpc/virnetsshsession.c
src/rpc/virnettlscontext.c

index 678e6f78152b814331f0f7e7be964bf1ee8433f8..47a17d30f7c14f21bfe5922bfbea314be5339056 100644 (file)
@@ -343,8 +343,7 @@ virNetClientCheckKeyExists(const char *homedir,
 {
     char *path;
 
-    if (virAsprintf(&path, "%s/.ssh/%s", homedir, name) < 0)
-        return -1;
+    path = g_strdup_printf("%s/.ssh/%s", homedir, name);
 
     if (!(virFileExists(path))) {
         VIR_FREE(path);
@@ -565,10 +564,8 @@ virNetClientPtr virNetClientNewLibssh(const char *host,
         knownhosts = g_strdup(knownHostsPath);
     } else {
         confdir = virGetUserConfigDirectory();
-        if (confdir) {
-            if (virAsprintf(&knownhosts, "%s/known_hosts", confdir) < 0)
-                goto cleanup;
-        }
+        if (confdir)
+            knownhosts = g_strdup_printf("%s/known_hosts", confdir);
     }
 
     if (privkeyPath) {
@@ -602,16 +599,10 @@ virNetClientPtr virNetClientNewLibssh(const char *host,
     if (!(nc = virBufferContentAndReset(&buf)))
         goto no_memory;
 
-    if (virAsprintf(&command,
-         "sh -c "
-         "'if '%s' -q 2>&1 | grep \"requires an argument\" >/dev/null 2>&1; then "
-             "ARG=-q0;"
-         "else "
-             "ARG=;"
-         "fi;"
-         "'%s' $ARG -U %s'",
-         nc, nc, socketPath) < 0)
-        goto cleanup;
+    command = g_strdup_printf("sh -c "
+                              "'if '%s' -q 2>&1 | grep \"requires an argument\" >/dev/null 2>&1; then "
+                              "ARG=-q0;" "else " "ARG=;" "fi;" "'%s' $ARG -U %s'", nc, nc,
+                              socketPath);
 
     if (virNetSocketNewConnectLibssh(host, port,
                                      family,
index c70838398c9eb26a1825434c2fdee60a0c33f675..2312939cdc8da3cfbeca2c28044d2942e047c8b9 100644 (file)
@@ -341,15 +341,8 @@ virNetLibsshCheckHostKey(virNetLibsshSessionPtr sess)
             if (!keyhashstr)
                 return -1;
 
-            if (virAsprintf(&tmp,
-                            _("Accept SSH host key with hash '%s' for "
-                              "host '%s:%d' (%s/%s)?"),
-                            keyhashstr,
-                            sess->hostname, sess->port,
-                            "y", "n") < 0) {
-                ssh_string_free_char(keyhashstr);
-                return -1;
-            }
+            tmp = g_strdup_printf(_("Accept SSH host key with hash '%s' for " "host '%s:%d' (%s/%s)?"),
+                                  keyhashstr, sess->hostname, sess->port, "y", "n");
             askKey.prompt = tmp;
 
             if (sess->cred->cb(&askKey, 1, sess->cred->cbdata)) {
@@ -530,10 +523,7 @@ virNetLibsshAuthenticatePrivkey(virNetLibsshSessionPtr sess,
 
     VIR_DEBUG("sess=%p", sess);
 
-    if (virAsprintf(&tmp, "%s.pub", priv->filename) < 0) {
-        err = SSH_AUTH_ERROR;
-        goto error;
-    }
+    tmp = g_strdup_printf("%s.pub", priv->filename);
 
     /* try to open the public part of the private key */
     ret = ssh_pki_import_pubkey_file(tmp, &public_key);
index d9fa38d5a69a79599aa83cfbcc5cb7576c6f975d..5f8b9f5c52399cd062a341bc1319bd092505ff27 100644 (file)
@@ -698,8 +698,7 @@ int virNetSocketNewConnectUNIX(const char *path,
             goto cleanup;
         }
 
-        if (virAsprintf(&lockpath, "%s/%s.lock", rundir, binname) < 0)
-            goto cleanup;
+        lockpath = g_strdup_printf("%s/%s.lock", rundir, binname);
 
         if ((lockfd = open(lockpath, O_RDWR | O_CREAT, 0600)) < 0 ||
             virSetCloseExec(lockfd) < 0) {
index 199043f40862a33af69fafee97d175e47d9f6250..ed8e9488355848035fa6f68e386f942794659ade 100644 (file)
@@ -362,15 +362,8 @@ virNetSSHCheckHostKey(virNetSSHSessionPtr sess)
             keyhashstr = virBufferContentAndReset(&buff);
 
             askKey.type = VIR_CRED_ECHOPROMPT;
-            if (virAsprintf((char **)&askKey.prompt,
-                            _("Accept SSH host key with hash '%s' for "
-                              "host '%s:%d' (%s/%s)?"),
-                            keyhashstr,
-                            sess->hostname, sess->port,
-                            "y", "n") < 0) {
-                VIR_FREE(keyhashstr);
-                return -1;
-            }
+            askKey.prompt = g_strdup_printf(_("Accept SSH host key with hash '%s' for " "host '%s:%d' (%s/%s)?"),
+                                            keyhashstr, sess->hostname, sess->port, "y", "n");
 
             if (sess->cred->cb(&askKey, 1, sess->cred->cbdata)) {
                 virReportError(VIR_ERR_SSH, "%s",
@@ -628,10 +621,8 @@ virNetSSHAuthenticatePrivkey(virNetSSHSessionPtr sess,
         return -1;
     }
 
-    if (virAsprintf((char **)&retr_passphrase.prompt,
-                    _("Passphrase for key '%s'"),
-                    priv->filename) < 0)
-        return -1;
+    retr_passphrase.prompt = g_strdup_printf(_("Passphrase for key '%s'"),
+                                             priv->filename);
 
     if (sess->cred->cb(&retr_passphrase, 1, sess->cred->cbdata)) {
         virReportError(VIR_ERR_SSH, "%s",
index 2420ad8681cebf18ff7dbfd68b0f7f66f25fbaf5..7163775f74c2e34d61c977b043b4d03b6b5c3881 100644 (file)
@@ -796,19 +796,13 @@ static int virNetTLSContextLocateCredentials(const char *pkipath,
      */
     if (pkipath) {
         VIR_DEBUG("Told to use TLS credentials in %s", pkipath);
-        if ((virAsprintf(cacert, "%s/%s", pkipath,
-                         "cacert.pem")) < 0)
-            goto error;
-        if ((virAsprintf(cacrl, "%s/%s", pkipath,
-                         "cacrl.pem")) < 0)
-            goto error;
-        if ((virAsprintf(key, "%s/%s", pkipath,
-                         isServer ? "serverkey.pem" : "clientkey.pem")) < 0)
-            goto error;
+        *cacert = g_strdup_printf("%s/%s", pkipath, "cacert.pem");
+        *cacrl = g_strdup_printf("%s/%s", pkipath, "cacrl.pem");
+        *key = g_strdup_printf("%s/%s", pkipath,
+                               isServer ? "serverkey.pem" : "clientkey.pem");
 
-        if ((virAsprintf(cert, "%s/%s", pkipath,
-                         isServer ? "servercert.pem" : "clientcert.pem")) < 0)
-             goto error;
+        *cert = g_strdup_printf("%s/%s", pkipath,
+                                isServer ? "servercert.pem" : "clientcert.pem");
     } else if (tryUserPkiPath) {
         /* Check to see if $HOME/.pki contains at least one of the
          * files and if so, use that
@@ -818,26 +812,19 @@ static int virNetTLSContextLocateCredentials(const char *pkipath,
         if (!userdir)
             goto error;
 
-        if (virAsprintf(&user_pki_path, "%s/.pki/libvirt", userdir) < 0)
-            goto error;
+        user_pki_path = g_strdup_printf("%s/.pki/libvirt", userdir);
 
         VIR_DEBUG("Trying to find TLS user credentials in %s", user_pki_path);
 
-        if ((virAsprintf(cacert, "%s/%s", user_pki_path,
-                         "cacert.pem")) < 0)
-            goto error;
+        *cacert = g_strdup_printf("%s/%s", user_pki_path, "cacert.pem");
 
-        if ((virAsprintf(cacrl, "%s/%s", user_pki_path,
-                         "cacrl.pem")) < 0)
-            goto error;
+        *cacrl = g_strdup_printf("%s/%s", user_pki_path, "cacrl.pem");
 
-        if ((virAsprintf(key, "%s/%s", user_pki_path,
-                         isServer ? "serverkey.pem" : "clientkey.pem")) < 0)
-            goto error;
+        *key = g_strdup_printf("%s/%s", user_pki_path,
+                               isServer ? "serverkey.pem" : "clientkey.pem");
 
-        if ((virAsprintf(cert, "%s/%s", user_pki_path,
-                         isServer ? "servercert.pem" : "clientcert.pem")) < 0)
-            goto error;
+        *cert = g_strdup_printf("%s/%s", user_pki_path,
+                                isServer ? "servercert.pem" : "clientcert.pem");
 
         /*
          * If some of the files can't be found, fallback