[libvirt] [PATCH] rpc: Refactor cleanup paths in virNetLibsshAuthenticatePassword

Peter Krempa posted 1 patch 5 years ago
Test syntax-check passed
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/libvirt tags/patchew/b5027ef809c73498169f4cd1547354e1970b9c45.1554219460.git.pkrempa@redhat.com
src/rpc/virnetlibsshsession.c | 33 ++++++++++++---------------------
1 file changed, 12 insertions(+), 21 deletions(-)
[libvirt] [PATCH] rpc: Refactor cleanup paths in virNetLibsshAuthenticatePassword
Posted by Peter Krempa 5 years ago
Now that the memory disposal is handled automatically we can simplify
the cleanup paths. In this case it's not as simple as sometimes the
value of the called function is returned.

While at it fix the initialization value of the returned variable.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
---
 src/rpc/virnetlibsshsession.c | 33 ++++++++++++---------------------
 1 file changed, 12 insertions(+), 21 deletions(-)

diff --git a/src/rpc/virnetlibsshsession.c b/src/rpc/virnetlibsshsession.c
index b9143239b9..9bf25c9caa 100644
--- a/src/rpc/virnetlibsshsession.c
+++ b/src/rpc/virnetlibsshsession.c
@@ -606,25 +606,22 @@ virNetLibsshAuthenticatePassword(virNetLibsshSessionPtr sess,
                                  virNetLibsshAuthMethodPtr priv)
 {
     const char *errmsg;
-    int ret = -1;
+    int rc = SSH_AUTH_ERROR;

     VIR_DEBUG("sess=%p", sess);

     if (priv->password) {
         /* tunelled password authentication */
-        if ((ret = ssh_userauth_password(sess->session, NULL,
-                                         priv->password)) == 0) {
-            ret = SSH_AUTH_SUCCESS;
-            goto cleanup;
-        }
+        if ((rc = ssh_userauth_password(sess->session, NULL,
+                                        priv->password)) == 0)
+            return SSH_AUTH_SUCCESS;
     } else {
         /* password authentication with interactive password request */
         if (!sess->cred || !sess->cred->cb) {
             virReportError(VIR_ERR_LIBSSH, "%s",
                            _("Can't perform authentication: "
                              "Authentication callback not provided"));
-            ret = SSH_AUTH_ERROR;
-            goto cleanup;
+            return SSH_AUTH_ERROR;
         }

         /* Try the authenticating the set amount of times. The server breaks the
@@ -634,19 +631,15 @@ virNetLibsshAuthenticatePassword(virNetLibsshSessionPtr sess,

             if (!(password = virAuthGetPasswordPath(sess->authPath, sess->cred,
                                                     "ssh", sess->username,
-                                                    sess->hostname))) {
-                ret = SSH_AUTH_ERROR;
-                goto cleanup;
-            }
+                                                    sess->hostname)))
+                return SSH_AUTH_ERROR;

             /* tunelled password authentication */
-            if ((ret = ssh_userauth_password(sess->session, NULL,
-                                             password)) == 0) {
-                ret = SSH_AUTH_SUCCESS;
-                goto cleanup;
-            }
+            if ((rc = ssh_userauth_password(sess->session, NULL,
+                                            password)) == 0)
+                return SSH_AUTH_SUCCESS;

-            if (ret != SSH_AUTH_DENIED)
+            if (rc != SSH_AUTH_DENIED)
                 break;
         }
     }
@@ -655,9 +648,7 @@ virNetLibsshAuthenticatePassword(virNetLibsshSessionPtr sess,
     errmsg = ssh_get_error(sess->session);
     virReportError(VIR_ERR_AUTH_FAILED,
                    _("authentication failed: %s"), errmsg);
-
- cleanup:
-    return ret;
+    return rc;
 }

 /* perform keyboard interactive authentication
-- 
2.20.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH] rpc: Refactor cleanup paths in virNetLibsshAuthenticatePassword
Posted by Ján Tomko 5 years ago
On Tue, Apr 02, 2019 at 05:37:40PM +0200, Peter Krempa wrote:
>Now that the memory disposal is handled automatically we can simplify
>the cleanup paths. In this case it's not as simple as sometimes the
>value of the called function is returned.
>
>While at it fix the initialization value of the returned variable.
>
>Signed-off-by: Peter Krempa <pkrempa@redhat.com>
>---
> src/rpc/virnetlibsshsession.c | 33 ++++++++++++---------------------
> 1 file changed, 12 insertions(+), 21 deletions(-)
>

Reviewed-by: Ján Tomko <jtomko@redhat.com>

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