sysdeps/posix/getaddrinfo: Return early on invalid address family

Message ID 20191124210129.873243-1-tim.ruehsen@gmx.de
State Committed
Headers

Commit Message

Tim Rühsen Nov. 24, 2019, 9:01 p.m. UTC
  Check address family before expensive function call (__check_pf).
---
 sysdeps/posix/getaddrinfo.c | 40 +++++++++++++++++--------------------
 1 file changed, 18 insertions(+), 22 deletions(-)

--
2.24.0
  

Comments

Florian Weimer Nov. 26, 2019, 10:30 a.m. UTC | #1
* Tim Rühsen:

> Check address family before expensive function call (__check_pf).

I have pushed this for you.  I did not check your copyright assignment
status because the changes are mostly reindentation.  But if you don't
have paperwork in place, this is probably the last contribution we can
accept from you.  Sorry about that.

<https://sourceware.org/glibc/wiki/Contribution%20checklist#FSF_copyright_Assignment>
  
Tim Rühsen Nov. 26, 2019, 10:35 a.m. UTC | #2
On 11/26/19 11:30 AM, Florian Weimer wrote:
> * Tim Rühsen:
> 
>> Check address family before expensive function call (__check_pf).
> 
> I have pushed this for you.  I did not check your copyright assignment
> status because the changes are mostly reindentation.  But if you don't
> have paperwork in place, this is probably the last contribution we can
> accept from you.  Sorry about that.
> 
> <https://sourceware.org/glibc/wiki/Contribution%20checklist#FSF_copyright_Assignment>

Thank you for heads up, Florian !

I will sign the FSF copyright assignment for GNU glibc in the next days
(have to check a few things first).

Regards, Tim
  

Patch

diff --git a/sysdeps/posix/getaddrinfo.c b/sysdeps/posix/getaddrinfo.c
index afdcdf0a3b..c67c5a955c 100644
--- a/sysdeps/posix/getaddrinfo.c
+++ b/sysdeps/posix/getaddrinfo.c
@@ -2186,6 +2186,10 @@  getaddrinfo (const char *name, const char *service,
   if ((hints->ai_flags & AI_CANONNAME) && name == NULL)
     return EAI_BADFLAGS;

+  if (hints->ai_family != AF_UNSPEC && hints->ai_family != AF_INET
+      && hints->ai_family != AF_INET6)
+    return EAI_FAMILY;
+
   struct in6addrinfo *in6ai = NULL;
   size_t in6ailen = 0;
   bool seen_ipv4 = false;
@@ -2244,33 +2248,25 @@  getaddrinfo (const char *name, const char *service,
     pservice = NULL;

   struct addrinfo **end = &p;
-
   unsigned int naddrs = 0;
-  if (hints->ai_family == AF_UNSPEC || hints->ai_family == AF_INET
-      || hints->ai_family == AF_INET6)
-    {
-      struct scratch_buffer tmpbuf;
-      scratch_buffer_init (&tmpbuf);
-      last_i = gaih_inet (name, pservice, hints, end, &naddrs, &tmpbuf);
-      scratch_buffer_free (&tmpbuf);
+  struct scratch_buffer tmpbuf;

-      if (last_i != 0)
-	{
-	  freeaddrinfo (p);
-	  __free_in6ai (in6ai);
+  scratch_buffer_init (&tmpbuf);
+  last_i = gaih_inet (name, pservice, hints, end, &naddrs, &tmpbuf);
+  scratch_buffer_free (&tmpbuf);

-	  return -last_i;
-	}
-      while (*end)
-	{
-	  end = &((*end)->ai_next);
-	  ++nresults;
-	}
-    }
-  else
+  if (last_i != 0)
     {
+      freeaddrinfo (p);
       __free_in6ai (in6ai);
-      return EAI_FAMILY;
+
+      return -last_i;
+    }
+
+  while (*end)
+    {
+      end = &((*end)->ai_next);
+      ++nresults;
     }

   if (naddrs > 1)