]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
virnetsockettest: refactor checkProtocols
authorJán Tomko <jtomko@redhat.com>
Sat, 4 Sep 2021 18:45:33 +0000 (20:45 +0200)
committerJán Tomko <jtomko@redhat.com>
Wed, 8 Sep 2021 14:19:55 +0000 (16:19 +0200)
Reduce variable scope, use g_auto and remove pointless labels.

Signed-off-by: Ján Tomko <jtomko@redhat.com>
Reviewed-by: Pavel Hrdina <phrdina@redhat.com>
tests/virnetsockettest.c

index 8059c6cbb06e05cca0534ad28209b9aad8361361..1de3771ad4003df215ec358f32c85358dcd60784 100644 (file)
@@ -47,28 +47,29 @@ checkProtocols(bool *hasIPv4, bool *hasIPv6,
 {
     struct sockaddr_in in4;
     struct sockaddr_in6 in6;
-    int s4 = -1, s6 = -1;
     size_t i;
-    int ret = -1;
 
     *freePort = 0;
     if (virNetSocketCheckProtocols(hasIPv4, hasIPv6) < 0)
         return -1;
 
     for (i = 0; i < 50; i++) {
+        VIR_AUTOCLOSE s4 = -1;
+        VIR_AUTOCLOSE s6 = -1;
+
         if (*hasIPv4) {
             if ((s4 = socket(AF_INET, SOCK_STREAM, 0)) < 0)
-                goto cleanup;
+                return -1;
         }
 
         if (*hasIPv6) {
             int only = 1;
 
             if ((s6 = socket(AF_INET6, SOCK_STREAM, 0)) < 0)
-                goto cleanup;
+                return -1;
 
             if (setsockopt(s6, IPPROTO_IPV6, IPV6_V6ONLY, &only, sizeof(only)) < 0)
-                goto cleanup;
+                return -1;
         }
 
         memset(&in4, 0, sizeof(in4));
@@ -84,22 +85,18 @@ checkProtocols(bool *hasIPv4, bool *hasIPv6,
         if (*hasIPv4) {
             if (bind(s4, (struct sockaddr *)&in4, sizeof(in4)) < 0) {
                 if (errno == EADDRINUSE) {
-                    VIR_FORCE_CLOSE(s4);
-                    VIR_FORCE_CLOSE(s6);
                     continue;
                 }
-                goto cleanup;
+                return -1;
             }
         }
 
         if (*hasIPv6) {
             if (bind(s6, (struct sockaddr *)&in6, sizeof(in6)) < 0) {
                 if (errno == EADDRINUSE) {
-                    VIR_FORCE_CLOSE(s4);
-                    VIR_FORCE_CLOSE(s6);
                     continue;
                 }
-                goto cleanup;
+                return -1;
             }
         }
 
@@ -109,12 +106,7 @@ checkProtocols(bool *hasIPv4, bool *hasIPv6,
 
     VIR_DEBUG("Choose port %d", *freePort);
 
-    ret = 0;
-
- cleanup:
-    VIR_FORCE_CLOSE(s4);
-    VIR_FORCE_CLOSE(s6);
-    return ret;
+    return 0;
 }
 
 struct testClientData {