[libvirt PATCH 6/6] virnetsockettest: refactor checkProtocols

Ján Tomko jtomko at redhat.com
Sat Sep 4 19:34:52 UTC 2021


Reduce variable scope, use g_auto and remove pointless labels.

Signed-off-by: Ján Tomko <jtomko at redhat.com>
---
 tests/virnetsockettest.c | 26 +++++++++-----------------
 1 file changed, 9 insertions(+), 17 deletions(-)

diff --git a/tests/virnetsockettest.c b/tests/virnetsockettest.c
index 8059c6cbb0..1de3771ad4 100644
--- a/tests/virnetsockettest.c
+++ b/tests/virnetsockettest.c
@@ -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 {
-- 
2.31.1




More information about the libvir-list mailing list