diff mbox series

[4/5] gaih_inet: Consolidate got_port code

Message ID 20210803212919.3059194-5-siddhesh@sourceware.org
State New
Headers show
Series getaddrinfo spaghetti cleanups | expand

Commit Message

Siddhesh Poyarekar Aug. 3, 2021, 9:29 p.m. UTC
Refactor the code to remove the unnecessary got_port goto across
conditional branches.
---
 sysdeps/posix/getaddrinfo.c | 105 +++++++++++++++++-------------------
 1 file changed, 48 insertions(+), 57 deletions(-)
diff mbox series

Patch

diff --git a/sysdeps/posix/getaddrinfo.c b/sysdeps/posix/getaddrinfo.c
index fd05d53c1a..220cd41cde 100644
--- a/sysdeps/posix/getaddrinfo.c
+++ b/sysdeps/posix/getaddrinfo.c
@@ -359,65 +359,12 @@  gaih_inet (const char *name, const struct gaih_service *service,
 	}
     }
 
-  int port = 0;
-  if (service != NULL)
-    {
-      if ((tp->protoflag & GAI_PROTO_NOSERVICE) != 0)
-	return -EAI_SERVICE;
-
-      if (service->num < 0)
-	{
-	  if (tp->name[0])
-	    {
-	      st = (struct gaih_servtuple *)
-		alloca_account (sizeof (struct gaih_servtuple), alloca_used);
-
-	      int rc = gaih_inet_serv (service->name, tp, req, st, tmpbuf);
-	      if (__glibc_unlikely (rc != 0))
-		return rc;
-	    }
-	  else
-	    {
-	      struct gaih_servtuple **pst = &st;
-	      for (tp++; tp->name[0]; tp++)
-		{
-		  struct gaih_servtuple *newp;
-
-		  if ((tp->protoflag & GAI_PROTO_NOSERVICE) != 0)
-		    continue;
+  if (service != NULL && (tp->protoflag & GAI_PROTO_NOSERVICE) != 0)
+    return -EAI_SERVICE;
 
-		  if (req->ai_socktype != 0
-		      && req->ai_socktype != tp->socktype)
-		    continue;
-		  if (req->ai_protocol != 0
-		      && !(tp->protoflag & GAI_PROTO_PROTOANY)
-		      && req->ai_protocol != tp->protocol)
-		    continue;
-
-		  newp = (struct gaih_servtuple *)
-		    alloca_account (sizeof (struct gaih_servtuple),
-				    alloca_used);
-
-		  if (gaih_inet_serv (service->name,
-				      tp, req, newp, tmpbuf) != 0)
-		    continue;
-
-		  *pst = newp;
-		  pst = &(newp->next);
-		}
-	      if (st == (struct gaih_servtuple *) &nullserv)
-		return -EAI_SERVICE;
-	    }
-	}
-      else
-	{
-	  port = htons (service->num);
-	  goto got_port;
-	}
-    }
-  else
+  if (service == NULL || service->num >= 0)
     {
-    got_port:
+      int port = service != NULL ? htons (service->num) : 0;
 
       if (req->ai_socktype || req->ai_protocol)
 	{
@@ -450,6 +397,50 @@  gaih_inet (const char *name, const struct gaih_service *service,
 	      }
 	}
     }
+  else
+    {
+      if (tp->name[0])
+	{
+	  st = (struct gaih_servtuple *)
+	    alloca_account (sizeof (struct gaih_servtuple), alloca_used);
+
+	  int rc = gaih_inet_serv (service->name, tp, req, st, tmpbuf);
+	  if (__glibc_unlikely (rc != 0))
+	    return rc;
+	}
+      else
+	{
+	  struct gaih_servtuple **pst = &st;
+	  for (tp++; tp->name[0]; tp++)
+	    {
+	      struct gaih_servtuple *newp;
+
+	      if ((tp->protoflag & GAI_PROTO_NOSERVICE) != 0)
+		continue;
+
+	      if (req->ai_socktype != 0
+		  && req->ai_socktype != tp->socktype)
+		continue;
+	      if (req->ai_protocol != 0
+		  && !(tp->protoflag & GAI_PROTO_PROTOANY)
+		  && req->ai_protocol != tp->protocol)
+		continue;
+
+	      newp = (struct gaih_servtuple *)
+		alloca_account (sizeof (struct gaih_servtuple),
+				alloca_used);
+
+	      if (gaih_inet_serv (service->name,
+				  tp, req, newp, tmpbuf) != 0)
+		continue;
+
+	      *pst = newp;
+	      pst = &(newp->next);
+	    }
+	  if (st == (struct gaih_servtuple *) &nullserv)
+	    return -EAI_SERVICE;
+	}
+    }
 
   bool malloc_name = false;
   struct gaih_addrtuple *addrmem = NULL;