29e444
commit 1cef1b19089528db11f221e938f60b9b048945d7
29e444
Author: Andreas Schwab <schwab@suse.de>
29e444
Date:   Thu Mar 21 15:50:27 2013 +0100
29e444
29e444
    Fix stack overflow in getaddrinfo with many results
29e444
12745e
diff --git glibc-2.17-c758a686/sysdeps/posix/getaddrinfo.c glibc-2.17-c758a686/sysdeps/posix/getaddrinfo.c
29e444
index d95c2d1..2309281 100644
12745e
--- glibc-2.17-c758a686/sysdeps/posix/getaddrinfo.c
12745e
+++ glibc-2.17-c758a686/sysdeps/posix/getaddrinfo.c
29e444
@@ -2489,11 +2489,27 @@ getaddrinfo (const char *name, const char *service,
29e444
       __typeof (once) old_once = once;
29e444
       __libc_once (once, gaiconf_init);
29e444
       /* Sort results according to RFC 3484.  */
29e444
-      struct sort_result results[nresults];
29e444
-      size_t order[nresults];
29e444
+      struct sort_result *results;
29e444
+      size_t *order;
29e444
       struct addrinfo *q;
29e444
       struct addrinfo *last = NULL;
29e444
       char *canonname = NULL;
29e444
+      bool malloc_results;
29e444
+
29e444
+      malloc_results
29e444
+	= !__libc_use_alloca (nresults * (sizeof (*results) + sizeof (size_t)));
29e444
+      if (malloc_results)
29e444
+	{
29e444
+	  results = malloc (nresults * (sizeof (*results) + sizeof (size_t)));
29e444
+	  if (results == NULL)
29e444
+	    {
29e444
+	      __free_in6ai (in6ai);
29e444
+	      return EAI_MEMORY;
29e444
+	    }
29e444
+	}
29e444
+      else
29e444
+	results = alloca (nresults * (sizeof (*results) + sizeof (size_t)));
29e444
+      order = (size_t *) (results + nresults);
29e444
 
29e444
       /* Now we definitely need the interface information.  */
29e444
       if (! check_pf_called)
29e444
@@ -2664,6 +2680,9 @@ getaddrinfo (const char *name, const char *service,
29e444
 
29e444
       /* Fill in the canonical name into the new first entry.  */
29e444
       p->ai_canonname = canonname;
29e444
+
29e444
+      if (malloc_results)
29e444
+	free (results);
29e444
     }
29e444
 
29e444
   __free_in6ai (in6ai);