ce426f
commit d5dd6189d506068ed11c8bfa1e1e9bffde04decd
ce426f
Author: Andreas Schwab <schwab@suse.de>
ce426f
Date:   Mon Jan 21 17:41:28 2013 +0100
ce426f
ce426f
    Fix parsing of numeric hosts in gethostbyname_r
ce426f
ce426f
diff --git glibc-2.17-c758a686/nss/Makefile glibc-2.17-c758a686/nss/Makefile
ce426f
index 449a258..553eafa 100644
ce426f
--- glibc-2.17-c758a686/nss/Makefile
ce426f
+++ glibc-2.17-c758a686/nss/Makefile
ce426f
@@ -37,7 +37,8 @@ install-bin             := getent makedb
ce426f
 makedb-modules = xmalloc hash-string
ce426f
 extra-objs		+= $(makedb-modules:=.o)
ce426f
 
ce426f
-tests			= test-netdb tst-nss-test1 bug17079 tst-nss-getpwent
ce426f
+tests			= test-netdb tst-nss-test1 bug17079 tst-nss-getpwent \
ce426f
+			 test-digits-dots
ce426f
 xtests			= bug-erange
ce426f
 
ce426f
 include ../Makeconfig
ce426f
diff --git glibc-2.17-c758a686/nss/digits_dots.c glibc-2.17-c758a686/nss/digits_dots.c
ce426f
index 2b86295..e007ef4 100644
ce426f
--- glibc-2.17-c758a686/nss/digits_dots.c
ce426f
+++ glibc-2.17-c758a686/nss/digits_dots.c
ce426f
@@ -46,7 +46,10 @@ __nss_hostname_digits_dots (const char *name, struct hostent *resbuf,
ce426f
     {
ce426f
       if (h_errnop)
ce426f
 	*h_errnop = NETDB_INTERNAL;
ce426f
-      *result = NULL;
ce426f
+      if (buffer_size == NULL)
ce426f
+	*status = NSS_STATUS_TRYAGAIN;
ce426f
+      else
ce426f
+	*result = NULL;
ce426f
       return -1;
ce426f
     }
ce426f
 
ce426f
@@ -83,14 +86,16 @@ __nss_hostname_digits_dots (const char *name, struct hostent *resbuf,
ce426f
 	}
ce426f
 
ce426f
       size_needed = (sizeof (*host_addr)
ce426f
-		     + sizeof (*h_addr_ptrs) + strlen (name) + 1);
ce426f
+		     + sizeof (*h_addr_ptrs)
ce426f
+		     + sizeof (*h_alias_ptr) + strlen (name) + 1);
ce426f
 
ce426f
       if (buffer_size == NULL)
ce426f
         {
ce426f
 	  if (buflen < size_needed)
ce426f
 	    {
ce426f
+	      *status = NSS_STATUS_TRYAGAIN;
ce426f
 	      if (h_errnop != NULL)
ce426f
-		*h_errnop = TRY_AGAIN;
ce426f
+		*h_errnop = NETDB_INTERNAL;
ce426f
 	      __set_errno (ERANGE);
ce426f
 	      goto done;
ce426f
 	    }
ce426f
@@ -109,7 +114,7 @@ __nss_hostname_digits_dots (const char *name, struct hostent *resbuf,
ce426f
 	      *buffer_size = 0;
ce426f
 	      __set_errno (save);
ce426f
 	      if (h_errnop != NULL)
ce426f
-		*h_errnop = TRY_AGAIN;
ce426f
+		*h_errnop = NETDB_INTERNAL;
ce426f
 	      *result = NULL;
ce426f
 	      goto done;
ce426f
 	    }
ce426f
@@ -149,7 +154,9 @@ __nss_hostname_digits_dots (const char *name, struct hostent *resbuf,
ce426f
 		  if (! ok)
ce426f
 		    {
ce426f
 		      *h_errnop = HOST_NOT_FOUND;
ce426f
-		      if (buffer_size)
ce426f
+		      if (buffer_size == NULL)
ce426f
+			*status = NSS_STATUS_NOTFOUND;
ce426f
+		      else
ce426f
 			*result = NULL;
ce426f
 		      goto done;
ce426f
 		    }
ce426f
@@ -190,7 +197,7 @@ __nss_hostname_digits_dots (const char *name, struct hostent *resbuf,
ce426f
 		  if (buffer_size == NULL)
ce426f
 		    *status = NSS_STATUS_SUCCESS;
ce426f
 		  else
ce426f
-		   *result = resbuf;
ce426f
+		    *result = resbuf;
ce426f
 		  goto done;
ce426f
 		}
ce426f
 
ce426f
@@ -201,15 +208,6 @@ __nss_hostname_digits_dots (const char *name, struct hostent *resbuf,
ce426f
 
ce426f
       if ((isxdigit (name[0]) && strchr (name, ':') != NULL) || name[0] == ':')
ce426f
 	{
ce426f
-	  const char *cp;
ce426f
-	  char *hostname;
ce426f
-	  typedef unsigned char host_addr_t[16];
ce426f
-	  host_addr_t *host_addr;
ce426f
-	  typedef char *host_addr_list_t[2];
ce426f
-	  host_addr_list_t *h_addr_ptrs;
ce426f
-	  size_t size_needed;
ce426f
-	  int addr_size;
ce426f
-
ce426f
 	  switch (af)
ce426f
 	    {
ce426f
 	    default:
ce426f
@@ -225,7 +223,10 @@ __nss_hostname_digits_dots (const char *name, struct hostent *resbuf,
ce426f
 	      /* This is not possible.  We cannot represent an IPv6 address
ce426f
 		 in an `struct in_addr' variable.  */
ce426f
 	      *h_errnop = HOST_NOT_FOUND;
ce426f
-	      *result = NULL;
ce426f
+	      if (buffer_size == NULL)
ce426f
+		*status = NSS_STATUS_NOTFOUND;
ce426f
+	      else
ce426f
+		*result = NULL;
ce426f
 	      goto done;
ce426f
 
ce426f
 	    case AF_INET6:
ce426f
@@ -233,42 +234,6 @@ __nss_hostname_digits_dots (const char *name, struct hostent *resbuf,
ce426f
 	      break;
ce426f
 	    }
ce426f
 
ce426f
-	  size_needed = (sizeof (*host_addr)
ce426f
-			 + sizeof (*h_addr_ptrs) + strlen (name) + 1);
ce426f
-
ce426f
-	  if (buffer_size == NULL && buflen < size_needed)
ce426f
-	    {
ce426f
-	      if (h_errnop != NULL)
ce426f
-		*h_errnop = TRY_AGAIN;
ce426f
-	      __set_errno (ERANGE);
ce426f
-	      goto done;
ce426f
-	    }
ce426f
-	  else if (buffer_size != NULL && *buffer_size < size_needed)
ce426f
-	    {
ce426f
-	      char *new_buf;
ce426f
-	      *buffer_size = size_needed;
ce426f
-	      new_buf = realloc (*buffer, *buffer_size);
ce426f
-
ce426f
-	      if (new_buf == NULL)
ce426f
-		{
ce426f
-		  save = errno;
ce426f
-		  free (*buffer);
ce426f
-		  __set_errno (save);
ce426f
-		  *buffer = NULL;
ce426f
-		  *buffer_size = 0;
ce426f
-		  *result = NULL;
ce426f
-		  goto done;
ce426f
-		}
ce426f
-	      *buffer = new_buf;
ce426f
-	    }
ce426f
-
ce426f
-	  memset (*buffer, '\0', size_needed);
ce426f
-
ce426f
-	  host_addr = (host_addr_t *) *buffer;
ce426f
-	  h_addr_ptrs = (host_addr_list_t *)
ce426f
-	    ((char *) host_addr + sizeof (*host_addr));
ce426f
-	  hostname = (char *) h_addr_ptrs + sizeof (*h_addr_ptrs);
ce426f
-
ce426f
 	  for (cp = name;; ++cp)
ce426f
 	    {
ce426f
 	      if (!*cp)
ce426f
@@ -281,7 +246,9 @@ __nss_hostname_digits_dots (const char *name, struct hostent *resbuf,
ce426f
 		  if (inet_pton (AF_INET6, name, host_addr) <= 0)
ce426f
 		    {
ce426f
 		      *h_errnop = HOST_NOT_FOUND;
ce426f
-		      if (buffer_size)
ce426f
+		      if (buffer_size == NULL)
ce426f
+			*status = NSS_STATUS_NOTFOUND;
ce426f
+		      else
ce426f
 			*result = NULL;
ce426f
 		      goto done;
ce426f
 		    }
ce426f
diff --git glibc-2.17-c758a686/nss/getXXbyYY_r.c glibc-2.17-c758a686/nss/getXXbyYY_r.c
ce426f
index 1067744..44d00f4 100644
ce426f
--- glibc-2.17-c758a686/nss/getXXbyYY_r.c
ce426f
+++ glibc-2.17-c758a686/nss/getXXbyYY_r.c
ce426f
@@ -179,6 +179,9 @@ INTERNAL (REENTRANT_NAME) (ADD_PARAMS, LOOKUP_TYPE *resbuf, char *buffer,
ce426f
     case -1:
ce426f
       return errno;
ce426f
     case 1:
ce426f
+#ifdef NEED_H_ERRNO
ce426f
+      any_service = true;
ce426f
+#endif
ce426f
       goto done;
ce426f
     }
ce426f
 #endif
ce426f
@@ -288,7 +291,7 @@ done:
ce426f
     /* This happens when we weren't able to use a service for reasons other
ce426f
        than the module not being found.  In such a case, we'd want to tell the
ce426f
        caller that errno has the real reason for failure.  */
ce426f
-      *h_errnop = NETDB_INTERNAL;
ce426f
+    *h_errnop = NETDB_INTERNAL;
ce426f
   else if (status != NSS_STATUS_SUCCESS && !any_service)
ce426f
     /* We were not able to use any service.  */
ce426f
     *h_errnop = NO_RECOVERY;
ce426f
diff --git glibc-2.17-c758a686/nss/test-digits-dots.c glibc-2.17-c758a686/nss/test-digits-dots.c
ce426f
new file mode 100644
ce426f
index 0000000..1efa344
ce426f
--- /dev/null
ce426f
+++ glibc-2.17-c758a686/nss/test-digits-dots.c
ce426f
@@ -0,0 +1,38 @@
ce426f
+/* Copyright (C) 2013 Free Software Foundation, Inc.
ce426f
+   This file is part of the GNU C Library.
ce426f
+
ce426f
+   The GNU C Library is free software; you can redistribute it and/or
ce426f
+   modify it under the terms of the GNU Lesser General Public
ce426f
+   License as published by the Free Software Foundation; either
ce426f
+   version 2.1 of the License, or (at your option) any later version.
ce426f
+
ce426f
+   The GNU C Library is distributed in the hope that it will be useful,
ce426f
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
ce426f
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
ce426f
+   Lesser General Public License for more details.
ce426f
+
ce426f
+   You should have received a copy of the GNU Lesser General Public
ce426f
+   License along with the GNU C Library; if not, see
ce426f
+   <http://www.gnu.org/licenses/>.  */
ce426f
+
ce426f
+/* Testcase for BZ #15014 */
ce426f
+
ce426f
+#include <stdlib.h>
ce426f
+#include <netdb.h>
ce426f
+#include <errno.h>
ce426f
+
ce426f
+static int
ce426f
+do_test (void)
ce426f
+{
ce426f
+  char buf[32];
ce426f
+  struct hostent *result = NULL;
ce426f
+  struct hostent ret;
ce426f
+  int h_err = 0;
ce426f
+  int err;
ce426f
+
ce426f
+  err = gethostbyname_r ("1.2.3.4", &ret, buf, sizeof (buf), &result, &h_err);
ce426f
+  return err == ERANGE && h_err == NETDB_INTERNAL ? EXIT_SUCCESS : EXIT_FAILURE;
ce426f
+}
ce426f
+
ce426f
+#define TEST_FUNCTION do_test ()
ce426f
+#include "../test-skeleton.c"