[PATCH v1 19/59] block/ssh.c: remove unneeded labels

Daniel Henrique Barboza posted 59 patches 6 years, 1 month ago
Maintainers: Aurelien Jarno <aurelien@aurel32.net>, Max Reitz <mreitz@redhat.com>, Gerd Hoffmann <kraxel@redhat.com>, Greg Kurz <groug@kaod.org>, Jason Wang <jasowang@redhat.com>, Corey Minyard <minyard@acm.org>, Aleksandar Markovic <amarkovic@wavecomp.com>, Kevin Wolf <kwolf@redhat.com>, Stefan Hajnoczi <stefanha@redhat.com>, David Hildenbrand <david@redhat.com>, "Dr. David Alan Gilbert" <dgilbert@redhat.com>, David Gibson <david@gibson.dropbear.id.au>, "Daniel P. Berrangé" <berrange@redhat.com>, Laurent Vivier <laurent@vivier.eu>, Wen Congyang <wencongyang2@huawei.com>, "Michael S. Tsirkin" <mst@redhat.com>, Cornelia Huck <cohuck@redhat.com>, "Richard W.M. Jones" <rjones@redhat.com>, Jeff Cody <codyprime@gmail.com>, Riku Voipio <riku.voipio@iki.fi>, Stefan Weil <sw@weilnetz.de>, Michael Roth <mdroth@linux.vnet.ibm.com>, Richard Henderson <rth@twiddle.net>, Eduardo Habkost <ehabkost@redhat.com>, Yuval Shaia <yuval.shaia.ml@gmail.com>, Fam Zheng <fam@euphon.net>, Xie Changlong <xiechanglong.d@gmail.com>, Marcel Apfelbaum <marcel.apfelbaum@gmail.com>, Halil Pasic <pasic@linux.ibm.com>, Juan Quintela <quintela@redhat.com>, John Snow <jsnow@redhat.com>, Dmitry Fleytman <dmitry.fleytman@gmail.com>, Pavel Dovgalyuk <pavel.dovgaluk@ispras.ru>, Christian Borntraeger <borntraeger@de.ibm.com>, Aleksandar Rikalo <aleksandar.rikalo@rt-rk.com>, "Gonglei (Arei)" <arei.gonglei@huawei.com>, Guan Xuetao <gxt@mprc.pku.edu.cn>, Paolo Bonzini <pbonzini@redhat.com>
[PATCH v1 19/59] block/ssh.c: remove unneeded labels
Posted by Daniel Henrique Barboza 6 years, 1 month ago
The 'out' labels for check_host_key_knownhosts() and authenticate()
functions can be removed and, instead, call 'return' with the
appropriate return value. The 'ret' integer from both functions
could also be removed.

CC: Richard W.M. Jones <rjones@redhat.com>
CC: qemu-block@nongnu.org
Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
---
 block/ssh.c | 61 +++++++++++++++++------------------------------------
 1 file changed, 19 insertions(+), 42 deletions(-)

diff --git a/block/ssh.c b/block/ssh.c
index b4375cf7d2..e0c56d002a 100644
--- a/block/ssh.c
+++ b/block/ssh.c
@@ -276,7 +276,6 @@ static void ssh_parse_filename(const char *filename, QDict *options,
 
 static int check_host_key_knownhosts(BDRVSSHState *s, Error **errp)
 {
-    int ret;
 #ifdef HAVE_LIBSSH_0_8
     enum ssh_known_hosts_e state;
     int r;
@@ -295,7 +294,6 @@ static int check_host_key_knownhosts(BDRVSSHState *s, Error **errp)
         trace_ssh_check_host_key_knownhosts();
         break;
     case SSH_KNOWN_HOSTS_CHANGED:
-        ret = -EINVAL;
         r = ssh_get_server_publickey(s->session, &pubkey);
         if (r == 0) {
             r = ssh_get_publickey_hash(pubkey, SSH_PUBLICKEY_HASH_SHA256,
@@ -320,28 +318,23 @@ static int check_host_key_knownhosts(BDRVSSHState *s, Error **errp)
                        "host key does not match the one in known_hosts; this "
                        "may be a possible attack");
         }
-        goto out;
+        return -EINVAL;
     case SSH_KNOWN_HOSTS_OTHER:
-        ret = -EINVAL;
         error_setg(errp,
                    "host key for this server not found, another type exists");
-        goto out;
+        return -EINVAL;
     case SSH_KNOWN_HOSTS_UNKNOWN:
-        ret = -EINVAL;
         error_setg(errp, "no host key was found in known_hosts");
-        goto out;
+        return -EINVAL;
     case SSH_KNOWN_HOSTS_NOT_FOUND:
-        ret = -ENOENT;
         error_setg(errp, "known_hosts file not found");
-        goto out;
+        return -ENOENT;
     case SSH_KNOWN_HOSTS_ERROR:
-        ret = -EINVAL;
         error_setg(errp, "error while checking the host");
-        goto out;
+        return -EINVAL;
     default:
-        ret = -EINVAL;
         error_setg(errp, "error while checking for known server (%d)", state);
-        goto out;
+        return -EINVAL;
     }
 #else /* !HAVE_LIBSSH_0_8 */
     int state;
@@ -355,40 +348,31 @@ static int check_host_key_knownhosts(BDRVSSHState *s, Error **errp)
         trace_ssh_check_host_key_knownhosts();
         break;
     case SSH_SERVER_KNOWN_CHANGED:
-        ret = -EINVAL;
         error_setg(errp,
                    "host key does not match the one in known_hosts; this "
                    "may be a possible attack");
-        goto out;
+        return -EINVAL;
     case SSH_SERVER_FOUND_OTHER:
-        ret = -EINVAL;
         error_setg(errp,
                    "host key for this server not found, another type exists");
-        goto out;
+        return -EINVAL;
     case SSH_SERVER_FILE_NOT_FOUND:
-        ret = -ENOENT;
         error_setg(errp, "known_hosts file not found");
-        goto out;
+        return -ENOENT;
     case SSH_SERVER_NOT_KNOWN:
-        ret = -EINVAL;
         error_setg(errp, "no host key was found in known_hosts");
-        goto out;
+        return -EINVAL;
     case SSH_SERVER_ERROR:
-        ret = -EINVAL;
         error_setg(errp, "server error");
-        goto out;
+        return -EINVAL;
     default:
-        ret = -EINVAL;
         error_setg(errp, "error while checking for known server (%d)", state);
-        goto out;
+        return -EINVAL;
     }
 #endif /* !HAVE_LIBSSH_0_8 */
 
     /* known_hosts checking successful. */
-    ret = 0;
-
- out:
-    return ret;
+    return 0;
 }
 
 static unsigned hex2decimal(char ch)
@@ -501,20 +485,18 @@ static int check_host_key(BDRVSSHState *s, SshHostKeyCheck *hkc, Error **errp)
 
 static int authenticate(BDRVSSHState *s, Error **errp)
 {
-    int r, ret;
+    int r;
     int method;
 
     /* Try to authenticate with the "none" method. */
     r = ssh_userauth_none(s->session, NULL);
     if (r == SSH_AUTH_ERROR) {
-        ret = -EPERM;
         session_error_setg(errp, s, "failed to authenticate using none "
                                     "authentication");
-        goto out;
+        return -EPERM;
     } else if (r == SSH_AUTH_SUCCESS) {
         /* Authenticated! */
-        ret = 0;
-        goto out;
+        return 0;
     }
 
     method = ssh_userauth_list(s->session, NULL);
@@ -527,23 +509,18 @@ static int authenticate(BDRVSSHState *s, Error **errp)
     if (method & SSH_AUTH_METHOD_PUBLICKEY) {
         r = ssh_userauth_publickey_auto(s->session, NULL, NULL);
         if (r == SSH_AUTH_ERROR) {
-            ret = -EINVAL;
             session_error_setg(errp, s, "failed to authenticate using "
                                         "publickey authentication");
-            goto out;
+            return -EINVAL;
         } else if (r == SSH_AUTH_SUCCESS) {
             /* Authenticated! */
-            ret = 0;
-            goto out;
+            return 0;
         }
     }
 
-    ret = -EPERM;
     error_setg(errp, "failed to authenticate using publickey authentication "
                "and the identities held by your ssh-agent");
-
- out:
-    return ret;
+    return -EPERM;
 }
 
 static QemuOptsList ssh_runtime_opts = {
-- 
2.24.1


Re: [PATCH v1 19/59] block/ssh.c: remove unneeded labels
Posted by Richard W.M. Jones 6 years, 1 month ago
On Mon, Jan 06, 2020 at 03:23:45PM -0300, Daniel Henrique Barboza wrote:
> The 'out' labels for check_host_key_knownhosts() and authenticate()
> functions can be removed and, instead, call 'return' with the
> appropriate return value. The 'ret' integer from both functions
> could also be removed.
> 
> CC: Richard W.M. Jones <rjones@redhat.com>
> CC: qemu-block@nongnu.org
> Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
> ---
>  block/ssh.c | 61 +++++++++++++++++------------------------------------
>  1 file changed, 19 insertions(+), 42 deletions(-)
> 
> diff --git a/block/ssh.c b/block/ssh.c
> index b4375cf7d2..e0c56d002a 100644
> --- a/block/ssh.c
> +++ b/block/ssh.c
> @@ -276,7 +276,6 @@ static void ssh_parse_filename(const char *filename, QDict *options,
>  
>  static int check_host_key_knownhosts(BDRVSSHState *s, Error **errp)
>  {
> -    int ret;
>  #ifdef HAVE_LIBSSH_0_8
>      enum ssh_known_hosts_e state;
>      int r;
> @@ -295,7 +294,6 @@ static int check_host_key_knownhosts(BDRVSSHState *s, Error **errp)
>          trace_ssh_check_host_key_knownhosts();
>          break;
>      case SSH_KNOWN_HOSTS_CHANGED:
> -        ret = -EINVAL;
>          r = ssh_get_server_publickey(s->session, &pubkey);
>          if (r == 0) {
>              r = ssh_get_publickey_hash(pubkey, SSH_PUBLICKEY_HASH_SHA256,
> @@ -320,28 +318,23 @@ static int check_host_key_knownhosts(BDRVSSHState *s, Error **errp)
>                         "host key does not match the one in known_hosts; this "
>                         "may be a possible attack");
>          }
> -        goto out;
> +        return -EINVAL;
>      case SSH_KNOWN_HOSTS_OTHER:
> -        ret = -EINVAL;
>          error_setg(errp,
>                     "host key for this server not found, another type exists");
> -        goto out;
> +        return -EINVAL;
>      case SSH_KNOWN_HOSTS_UNKNOWN:
> -        ret = -EINVAL;
>          error_setg(errp, "no host key was found in known_hosts");
> -        goto out;
> +        return -EINVAL;
>      case SSH_KNOWN_HOSTS_NOT_FOUND:
> -        ret = -ENOENT;
>          error_setg(errp, "known_hosts file not found");
> -        goto out;
> +        return -ENOENT;
>      case SSH_KNOWN_HOSTS_ERROR:
> -        ret = -EINVAL;
>          error_setg(errp, "error while checking the host");
> -        goto out;
> +        return -EINVAL;
>      default:
> -        ret = -EINVAL;
>          error_setg(errp, "error while checking for known server (%d)", state);
> -        goto out;
> +        return -EINVAL;
>      }
>  #else /* !HAVE_LIBSSH_0_8 */
>      int state;
> @@ -355,40 +348,31 @@ static int check_host_key_knownhosts(BDRVSSHState *s, Error **errp)
>          trace_ssh_check_host_key_knownhosts();
>          break;
>      case SSH_SERVER_KNOWN_CHANGED:
> -        ret = -EINVAL;
>          error_setg(errp,
>                     "host key does not match the one in known_hosts; this "
>                     "may be a possible attack");
> -        goto out;
> +        return -EINVAL;
>      case SSH_SERVER_FOUND_OTHER:
> -        ret = -EINVAL;
>          error_setg(errp,
>                     "host key for this server not found, another type exists");
> -        goto out;
> +        return -EINVAL;
>      case SSH_SERVER_FILE_NOT_FOUND:
> -        ret = -ENOENT;
>          error_setg(errp, "known_hosts file not found");
> -        goto out;
> +        return -ENOENT;
>      case SSH_SERVER_NOT_KNOWN:
> -        ret = -EINVAL;
>          error_setg(errp, "no host key was found in known_hosts");
> -        goto out;
> +        return -EINVAL;
>      case SSH_SERVER_ERROR:
> -        ret = -EINVAL;
>          error_setg(errp, "server error");
> -        goto out;
> +        return -EINVAL;
>      default:
> -        ret = -EINVAL;
>          error_setg(errp, "error while checking for known server (%d)", state);
> -        goto out;
> +        return -EINVAL;
>      }
>  #endif /* !HAVE_LIBSSH_0_8 */
>  
>      /* known_hosts checking successful. */
> -    ret = 0;
> -
> - out:
> -    return ret;
> +    return 0;
>  }
>  
>  static unsigned hex2decimal(char ch)
> @@ -501,20 +485,18 @@ static int check_host_key(BDRVSSHState *s, SshHostKeyCheck *hkc, Error **errp)
>  
>  static int authenticate(BDRVSSHState *s, Error **errp)
>  {
> -    int r, ret;
> +    int r;
>      int method;
>  
>      /* Try to authenticate with the "none" method. */
>      r = ssh_userauth_none(s->session, NULL);
>      if (r == SSH_AUTH_ERROR) {
> -        ret = -EPERM;
>          session_error_setg(errp, s, "failed to authenticate using none "
>                                      "authentication");
> -        goto out;
> +        return -EPERM;
>      } else if (r == SSH_AUTH_SUCCESS) {
>          /* Authenticated! */
> -        ret = 0;
> -        goto out;
> +        return 0;
>      }
>  
>      method = ssh_userauth_list(s->session, NULL);
> @@ -527,23 +509,18 @@ static int authenticate(BDRVSSHState *s, Error **errp)
>      if (method & SSH_AUTH_METHOD_PUBLICKEY) {
>          r = ssh_userauth_publickey_auto(s->session, NULL, NULL);
>          if (r == SSH_AUTH_ERROR) {
> -            ret = -EINVAL;
>              session_error_setg(errp, s, "failed to authenticate using "
>                                          "publickey authentication");
> -            goto out;
> +            return -EINVAL;
>          } else if (r == SSH_AUTH_SUCCESS) {
>              /* Authenticated! */
> -            ret = 0;
> -            goto out;
> +            return 0;
>          }
>      }
>  
> -    ret = -EPERM;
>      error_setg(errp, "failed to authenticate using publickey authentication "
>                 "and the identities held by your ssh-agent");
> -
> - out:
> -    return ret;
> +    return -EPERM;
>  }
>  
>  static QemuOptsList ssh_runtime_opts = {

I agree that the code is functionality the same after this change.
Don't know whether or not one style or the other is preferred by qemu,
but in any case:

Reviewed-by: Richard W.M. Jones <rjones@redhat.com>

Rich.

-- 
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
virt-top is 'top' for virtual machines.  Tiny program with many
powerful monitoring features, net stats, disk stats, logging, etc.
http://people.redhat.com/~rjones/virt-top