Blame SOURCES/glibc-rh677316-inet_pton.patch

51f0aa
Backport of
51f0aa
51f0aa
inet/if_index.c
51f0aa
inet/inet6_scopeid_pton.c
51f0aa
inet/inet_mkadr.c
51f0aa
inet/tst-inet6_scopeid_pton.c
51f0aa
resolv/inet_pton.c
51f0aa
resolv/tst-inet_pton.c
51f0aa
51f0aa
up to and including this upstream commit on the 2.26 branch:
51f0aa
51f0aa
commit 77db8772bd3f6f2bbad697dcf46861ce310f5b95
51f0aa
Author: Florian Weimer <fweimer@redhat.com>
51f0aa
Date:   Thu Aug 10 16:06:52 2017 +0200
51f0aa
51f0aa
    __inet6_scopeid_pton: Remove attribute_hidden, internal_function
51f0aa
51f0aa
This change makes the inet_pton parser more strict for IPv6 addresses
51f0aa
(upstream bug 16637).  The stricter __inet6_scopeid_pton parser is not
51f0aa
effective yet with this patch because it is not used by the library yet.
51f0aa
51f0aa
diff --git a/include/arpa/inet.h b/include/arpa/inet.h
51f0aa
index 63ece7029aa1d417..c3f28f2baaa2ed66 100644
51f0aa
--- a/include/arpa/inet.h
51f0aa
+++ b/include/arpa/inet.h
51f0aa
@@ -7,6 +7,9 @@ libc_hidden_proto (__inet_aton)
51f0aa
 libc_hidden_proto (inet_aton)
51f0aa
 libc_hidden_proto (inet_ntop)
51f0aa
 libc_hidden_proto (inet_pton)
51f0aa
-libc_hidden_proto (inet_makeaddr)
51f0aa
+extern __typeof (inet_pton) __inet_pton;
51f0aa
+libc_hidden_proto (__inet_pton)
51f0aa
+extern __typeof (inet_makeaddr) __inet_makeaddr;
51f0aa
+libc_hidden_proto (__inet_makeaddr)
51f0aa
 libc_hidden_proto (inet_netof)
51f0aa
 #endif
51f0aa
diff --git a/include/net/if.h b/include/net/if.h
51f0aa
index 1d862260c7e83977..6c4cbc96c3733a01 100644
51f0aa
--- a/include/net/if.h
51f0aa
+++ b/include/net/if.h
51f0aa
@@ -4,9 +4,13 @@
51f0aa
 
51f0aa
 #ifndef _ISOMAC
51f0aa
 libc_hidden_proto (if_nametoindex)
51f0aa
+extern __typeof (if_nametoindex) __if_nametoindex;
51f0aa
+libc_hidden_proto (__if_nametoindex)
51f0aa
 libc_hidden_proto (if_indextoname)
51f0aa
 libc_hidden_proto (if_nameindex)
51f0aa
 libc_hidden_proto (if_freenameindex)
51f0aa
+extern __typeof (if_freenameindex) __if_freenameindex;
51f0aa
+libc_hidden_proto (__if_freenameindex)
51f0aa
 #endif
51f0aa
 
51f0aa
 #endif
51f0aa
diff --git a/inet/Makefile b/inet/Makefile
51f0aa
index 768df21b92534a31..4abf2ddf6dfff327 100644
51f0aa
--- a/inet/Makefile
51f0aa
+++ b/inet/Makefile
51f0aa
@@ -45,7 +45,7 @@ routines := htonl htons		\
51f0aa
 	    in6_addr getnameinfo if_index ifaddrs inet6_option \
51f0aa
 	    getipv4sourcefilter setipv4sourcefilter \
51f0aa
 	    getsourcefilter setsourcefilter inet6_opt inet6_rth \
51f0aa
-	    deadline
51f0aa
+	    inet6_scopeid_pton deadline
51f0aa
 
51f0aa
 aux := check_pf check_native ifreq
51f0aa
 
51f0aa
@@ -57,6 +57,10 @@ tests := htontest test_ifindex tst-ntoa tst-ether_aton tst-network \
51f0aa
 # internal functions.
51f0aa
 tests-static += tst-deadline
51f0aa
 
51f0aa
+# tst-inet6_scopeid_pton also needs internal functions but does not
51f0aa
+# need to be linked statically.
51f0aa
+tests += tst-inet6_scopeid_pton
51f0aa
+
51f0aa
 include ../Rules
51f0aa
 
51f0aa
 ifeq ($(have-thread-library),yes)
51f0aa
diff --git a/inet/Versions b/inet/Versions
51f0aa
index 06507199a92b3c51..157559c44442259e 100644
51f0aa
--- a/inet/Versions
51f0aa
+++ b/inet/Versions
51f0aa
@@ -88,5 +88,8 @@ libc {
51f0aa
     # functions used in other libraries
51f0aa
     __internal_endnetgrent; __internal_getnetgrent_r;
51f0aa
     __internal_setnetgrent;
51f0aa
+
51f0aa
+   # Used from nscd.
51f0aa
+    __inet6_scopeid_pton;
51f0aa
   }
51f0aa
 }
51f0aa
diff --git a/inet/if_index.c b/inet/if_index.c
51f0aa
index 3d706766be62b91b..4604e0a35b890471 100644
51f0aa
--- a/inet/if_index.c
51f0aa
+++ b/inet/if_index.c
51f0aa
@@ -1,4 +1,4 @@
51f0aa
-/* Copyright (C) 1997,98,99,2000,02 Free Software Foundation, Inc.
51f0aa
+/* Copyright (C) 1997-2017 Free Software Foundation, Inc.
51f0aa
    This file is part of the GNU C Library.
51f0aa
 
51f0aa
    The GNU C Library is free software; you can redistribute it and/or
51f0aa
@@ -20,44 +20,41 @@
51f0aa
 #include <stddef.h>
51f0aa
 
51f0aa
 unsigned int
51f0aa
-if_nametoindex (const char *ifname)
51f0aa
+__if_nametoindex (const char *ifname)
51f0aa
 {
51f0aa
   __set_errno (ENOSYS);
51f0aa
   return 0;
51f0aa
 }
51f0aa
-libc_hidden_def (if_nametoindex)
51f0aa
+libc_hidden_def (__if_nametoindex)
51f0aa
+weak_alias (__if_nametoindex, if_nametoindex)
51f0aa
+libc_hidden_weak (if_nametoindex)
51f0aa
 stub_warning (if_nametoindex)
51f0aa
 
51f0aa
 char *
51f0aa
-if_indextoname (unsigned int ifindex, char *ifname)
51f0aa
+__if_indextoname (unsigned int ifindex, char *ifname)
51f0aa
 {
51f0aa
   __set_errno (ENOSYS);
51f0aa
   return NULL;
51f0aa
 }
51f0aa
-libc_hidden_def (if_indextoname)
51f0aa
+weak_alias (__if_indextoname, if_indextoname)
51f0aa
+libc_hidden_weak (if_indextoname)
51f0aa
 stub_warning (if_indextoname)
51f0aa
 
51f0aa
 void
51f0aa
-if_freenameindex (struct if_nameindex *ifn)
51f0aa
+__if_freenameindex (struct if_nameindex *ifn)
51f0aa
 {
51f0aa
 }
51f0aa
+libc_hidden_def (__if_freenameindex)
51f0aa
+weak_alias (__if_freenameindex, if_freenameindex)
51f0aa
+libc_hidden_weak (if_freenameindex)
51f0aa
 stub_warning (if_freenameindex)
51f0aa
 
51f0aa
 struct if_nameindex *
51f0aa
-if_nameindex (void)
51f0aa
+__if_nameindex (void)
51f0aa
 {
51f0aa
   __set_errno (ENOSYS);
51f0aa
   return NULL;
51f0aa
 }
51f0aa
+weak_alias (__if_nameindex, if_nameindex)
51f0aa
+libc_hidden_weak (if_nameindex)
51f0aa
 stub_warning (if_nameindex)
51f0aa
-
51f0aa
-#if 0
51f0aa
-void
51f0aa
-internal_function
51f0aa
-__protocol_available (int *have_inet, int *have_inet6)
51f0aa
-{
51f0aa
-  /* By default we assume that IPv4 is available, IPv6 not.  */
51f0aa
-  *have_inet = 1;
51f0aa
-  *have_inet6 = 0;
51f0aa
-}
51f0aa
-#endif
51f0aa
diff --git a/inet/inet6_scopeid_pton.c b/inet/inet6_scopeid_pton.c
51f0aa
new file mode 100644
51f0aa
index 0000000000000000..cc8803fa108f7bcb
51f0aa
--- /dev/null
51f0aa
+++ b/inet/inet6_scopeid_pton.c
51f0aa
@@ -0,0 +1,64 @@
51f0aa
+/* Convert an IPv6 scope ID from text to the internal representation.
51f0aa
+   Copyright (C) 2016-2017 Free Software Foundation, Inc.
51f0aa
+   This file is part of the GNU C Library.
51f0aa
+
51f0aa
+   The GNU C Library is free software; you can redistribute it and/or
51f0aa
+   modify it under the terms of the GNU Lesser General Public
51f0aa
+   License as published by the Free Software Foundation; either
51f0aa
+   version 2.1 of the License, or (at your option) any later version.
51f0aa
+
51f0aa
+   The GNU C Library is distributed in the hope that it will be useful,
51f0aa
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
51f0aa
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
51f0aa
+   Lesser General Public License for more details.
51f0aa
+
51f0aa
+   You should have received a copy of the GNU Lesser General Public
51f0aa
+   License along with the GNU C Library; if not, see
51f0aa
+   <http://www.gnu.org/licenses/>.  */
51f0aa
+
51f0aa
+#include <net-internal.h>
51f0aa
+
51f0aa
+#include <ctype.h>
51f0aa
+#include <errno.h>
51f0aa
+#include <locale.h>
51f0aa
+#include <net/if.h>
51f0aa
+#include <stdbool.h>
51f0aa
+#include <stdint.h>
51f0aa
+#include <stdlib.h>
51f0aa
+
51f0aa
+/* Parse SOURCE as a scope ID for ADDRESS.  Return 0 on success and -1
51f0aa
+   on error.  */
51f0aa
+int
51f0aa
+__inet6_scopeid_pton (const struct in6_addr *address, const char *scope,
51f0aa
+                      uint32_t *result)
51f0aa
+{
51f0aa
+  if (IN6_IS_ADDR_LINKLOCAL (address)
51f0aa
+      || IN6_IS_ADDR_MC_NODELOCAL (address)
51f0aa
+      || IN6_IS_ADDR_MC_LINKLOCAL (address))
51f0aa
+    {
51f0aa
+      uint32_t number = __if_nametoindex (scope);
51f0aa
+      if (number != 0)
51f0aa
+        {
51f0aa
+          *result = number;
51f0aa
+          return 0;
51f0aa
+        }
51f0aa
+    }
51f0aa
+
51f0aa
+  if (isdigit_l (scope[0], _nl_C_locobj_ptr))
51f0aa
+    {
51f0aa
+      char *end;
51f0aa
+      unsigned long long number
51f0aa
+        = ____strtoull_l_internal (scope, &end, /*base */ 10, /* group */ 0,
51f0aa
+                                   _nl_C_locobj_ptr);
51f0aa
+      if (*end == '\0' && number <= UINT32_MAX)
51f0aa
+        {
51f0aa
+          *result = number;
51f0aa
+          return 0;
51f0aa
+        }
51f0aa
+    }
51f0aa
+
51f0aa
+  __set_errno (EINVAL);
51f0aa
+  return -1;
51f0aa
+}
51f0aa
+
51f0aa
+libc_hidden_def (__inet6_scopeid_pton)
51f0aa
diff --git a/inet/inet_mkadr.c b/inet/inet_mkadr.c
51f0aa
index d8d92da768b2ede0..88faef74cf50b722 100644
51f0aa
--- a/inet/inet_mkadr.c
51f0aa
+++ b/inet/inet_mkadr.c
51f0aa
@@ -27,10 +27,6 @@
51f0aa
  * SUCH DAMAGE.
51f0aa
  */
51f0aa
 
51f0aa
-#if defined(LIBC_SCCS) && !defined(lint)
51f0aa
-static char sccsid[] = "@(#)inet_makeaddr.c	8.1 (Berkeley) 6/4/93";
51f0aa
-#endif /* LIBC_SCCS and not lint */
51f0aa
-
51f0aa
 #include <sys/param.h>
51f0aa
 #include <netinet/in.h>
51f0aa
 #include <arpa/inet.h>
51f0aa
@@ -40,8 +36,7 @@ static char sccsid[] = "@(#)inet_makeaddr.c	8.1 (Berkeley) 6/4/93";
51f0aa
  * building addresses stored in the ifnet structure.
51f0aa
  */
51f0aa
 struct in_addr
51f0aa
-inet_makeaddr(net, host)
51f0aa
-	in_addr_t net, host;
51f0aa
+__inet_makeaddr (in_addr_t net, in_addr_t host)
51f0aa
 {
51f0aa
 	struct in_addr in;
51f0aa
 
51f0aa
@@ -56,4 +51,5 @@ inet_makeaddr(net, host)
51f0aa
 	in.s_addr = htonl(in.s_addr);
51f0aa
 	return in;
51f0aa
 }
51f0aa
-libc_hidden_def (inet_makeaddr)
51f0aa
+libc_hidden_def (__inet_makeaddr)
51f0aa
+weak_alias (__inet_makeaddr, inet_makeaddr)
51f0aa
diff --git a/inet/net-internal.h b/inet/net-internal.h
51f0aa
index 58349f54ea126698..6c666752493811be 100644
51f0aa
--- a/inet/net-internal.h
51f0aa
+++ b/inet/net-internal.h
51f0aa
@@ -19,10 +19,15 @@
51f0aa
 #ifndef _NET_INTERNAL_H
51f0aa
 #define _NET_INTERNAL_H 1
51f0aa
 
51f0aa
+#include <arpa/inet.h>
51f0aa
 #include <stdbool.h>
51f0aa
 #include <stdint.h>
51f0aa
 #include <sys/time.h>
51f0aa
 
51f0aa
+int __inet6_scopeid_pton (const struct in6_addr *address,
51f0aa
+                          const char *scope, uint32_t *result);
51f0aa
+libc_hidden_proto (__inet6_scopeid_pton)
51f0aa
+
51f0aa
 /* Deadline handling for enforcing timeouts.
51f0aa
 
51f0aa
    Code should call __deadline_current_time to obtain the current time
51f0aa
diff --git a/inet/tst-inet6_scopeid_pton.c b/inet/tst-inet6_scopeid_pton.c
51f0aa
new file mode 100644
51f0aa
index 0000000000000000..8225b3b80abe6331
51f0aa
--- /dev/null
51f0aa
+++ b/inet/tst-inet6_scopeid_pton.c
51f0aa
@@ -0,0 +1,230 @@
51f0aa
+/* Tests for __inet6_scopeid_pton and IPv6 scopes in getaddrinfo.
51f0aa
+   Copyright (C) 2016-2017 Free Software Foundation, Inc.
51f0aa
+   This file is part of the GNU C Library.
51f0aa
+
51f0aa
+   The GNU C Library is free software; you can redistribute it and/or
51f0aa
+   modify it under the terms of the GNU Lesser General Public
51f0aa
+   License as published by the Free Software Foundation; either
51f0aa
+   version 2.1 of the License, or (at your option) any later version.
51f0aa
+
51f0aa
+   The GNU C Library is distributed in the hope that it will be useful,
51f0aa
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
51f0aa
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
51f0aa
+   Lesser General Public License for more details.
51f0aa
+
51f0aa
+   You should have received a copy of the GNU Lesser General Public
51f0aa
+   License along with the GNU C Library; if not, see
51f0aa
+   <http://www.gnu.org/licenses/>.  */
51f0aa
+
51f0aa
+#include <arpa/inet.h>
51f0aa
+#include <inttypes.h>
51f0aa
+#include <net-internal.h>
51f0aa
+#include <net/if.h>
51f0aa
+#include <netdb.h>
51f0aa
+#include <stdio.h>
51f0aa
+#include <stdlib.h>
51f0aa
+#include <string.h>
51f0aa
+#include <support/check.h>
51f0aa
+#include <support/support.h>
51f0aa
+#include <support/test-driver.h>
51f0aa
+
51f0aa
+/* An interface which is known to the system.  */
51f0aa
+static const char *interface_name;
51f0aa
+static uint32_t interface_index;
51f0aa
+
51f0aa
+/* Initiale the variables above.  */
51f0aa
+static void
51f0aa
+setup_interface (void)
51f0aa
+{
51f0aa
+  struct if_nameindex *list = if_nameindex ();
51f0aa
+  if (list != NULL && list[0].if_index != 0 && list[0].if_name[0] != '\0')
51f0aa
+    {
51f0aa
+      interface_name = list[0].if_name;
51f0aa
+      interface_index = list[0].if_index;
51f0aa
+    }
51f0aa
+}
51f0aa
+
51f0aa
+/* Convert ADDRESS to struct in6_addr.  */
51f0aa
+static struct in6_addr
51f0aa
+from_string (const char *address)
51f0aa
+{
51f0aa
+  struct in6_addr addr;
51f0aa
+  if (inet_pton (AF_INET6, address, &addr) != 1)
51f0aa
+    FAIL_EXIT1 ("inet_pton (\"%s\")", address);
51f0aa
+  return addr;
51f0aa
+}
51f0aa
+
51f0aa
+/* Invoke getaddrinfo to parse ADDRESS%SCOPE.  Return true if
51f0aa
+   getaddrinfo was successful.  */
51f0aa
+static bool
51f0aa
+call_gai (int family, const char *address, const char *scope,
51f0aa
+          struct sockaddr_in6 *result)
51f0aa
+{
51f0aa
+  struct addrinfo hints =
51f0aa
+    {
51f0aa
+      .ai_family = family,
51f0aa
+      .ai_flags = AI_NUMERICHOST,
51f0aa
+      .ai_socktype = SOCK_DGRAM,
51f0aa
+      .ai_protocol = IPPROTO_UDP,
51f0aa
+    };
51f0aa
+  char *fulladdr = xasprintf ("%s%%%s", address, scope);
51f0aa
+  struct addrinfo *ai = NULL;
51f0aa
+  int ret = getaddrinfo (fulladdr, NULL, &hints, &ai;;
51f0aa
+  if (ret == EAI_ADDRFAMILY || ret == EAI_NONAME)
51f0aa
+    {
51f0aa
+      if (test_verbose > 0)
51f0aa
+        printf ("info: getaddrinfo (\"%s\"): %s (%d)\n",
51f0aa
+                fulladdr, gai_strerror (ret), ret);
51f0aa
+      free (fulladdr);
51f0aa
+      return false;
51f0aa
+    }
51f0aa
+  if (ret != 0)
51f0aa
+    FAIL_EXIT1 ("getaddrinfo (\"%s\"): %s (%d)\n",
51f0aa
+                fulladdr, gai_strerror (ret), ret);
51f0aa
+  TEST_VERIFY_EXIT (ai != NULL);
51f0aa
+  TEST_VERIFY_EXIT (ai->ai_addrlen == sizeof (*result));
51f0aa
+  TEST_VERIFY (ai->ai_family == AF_INET6);
51f0aa
+  TEST_VERIFY (ai->ai_next == NULL);
51f0aa
+  memcpy (result, ai->ai_addr, sizeof (*result));
51f0aa
+  free (fulladdr);
51f0aa
+  freeaddrinfo (ai);
51f0aa
+  return true;
51f0aa
+}
51f0aa
+
51f0aa
+/* Verify that a successful call to getaddrinfo returned the expected
51f0aa
+   scope data.  */
51f0aa
+static void
51f0aa
+check_ai (const char *what, const char *addr_string, const char *scope_string,
51f0aa
+          const struct sockaddr_in6 *sa,
51f0aa
+          const struct in6_addr *addr, uint32_t scope)
51f0aa
+{
51f0aa
+  if (memcmp (addr, &sa->sin6_addr, sizeof (*addr)) != 0)
51f0aa
+    {
51f0aa
+      support_record_failure ();
51f0aa
+      printf ("error: getaddrinfo %s address mismatch for %s%%%s\n",
51f0aa
+              what, addr_string, scope_string);
51f0aa
+    }
51f0aa
+  if (sa->sin6_scope_id != scope)
51f0aa
+    {
51f0aa
+      support_record_failure ();
51f0aa
+      printf ("error: getaddrinfo %s scope mismatch for %s%%%s\n"
51f0aa
+              "  expected: %" PRIu32 "\n"
51f0aa
+              "  actual:   %" PRIu32 "\n",
51f0aa
+              what, addr_string, scope_string, scope, sa->sin6_scope_id);
51f0aa
+    }
51f0aa
+}
51f0aa
+
51f0aa
+/* Check a single address were we expected a failure.  */
51f0aa
+static void
51f0aa
+expect_failure (const char *address, const char *scope)
51f0aa
+{
51f0aa
+  if (test_verbose > 0)
51f0aa
+    printf ("info: expecting failure for %s%%%s\n", address, scope);
51f0aa
+  struct in6_addr addr = from_string (address);
51f0aa
+  uint32_t result = 1234;
51f0aa
+  if (__inet6_scopeid_pton (&addr, scope, &result) == 0)
51f0aa
+    {
51f0aa
+      support_record_failure ();
51f0aa
+      printf ("error: unexpected success for %s%%%s\n",
51f0aa
+              address, scope);
51f0aa
+    }
51f0aa
+  if (result != 1234)
51f0aa
+    {
51f0aa
+      support_record_failure ();
51f0aa
+      printf ("error: unexpected result update for %s%%%s\n",
51f0aa
+              address, scope);
51f0aa
+    }
51f0aa
+
51f0aa
+  struct sockaddr_in6 sa;
51f0aa
+  if (call_gai (AF_UNSPEC, address, scope, &sa))
51f0aa
+    {
51f0aa
+      support_record_failure ();
51f0aa
+      printf ("error: unexpected getaddrinfo success for %s%%%s (AF_UNSPEC)\n",
51f0aa
+              address, scope);
51f0aa
+    }
51f0aa
+  if (call_gai (AF_INET6, address, scope, &sa))
51f0aa
+    {
51f0aa
+      support_record_failure ();
51f0aa
+      printf ("error: unexpected getaddrinfo success for %s%%%s (AF_INET6)\n",
51f0aa
+              address, scope);
51f0aa
+    }
51f0aa
+}
51f0aa
+
51f0aa
+/* Check a single address were we expected a success.  */
51f0aa
+static void
51f0aa
+expect_success (const char *address, const char *scope, uint32_t expected)
51f0aa
+{
51f0aa
+  if (test_verbose > 0)
51f0aa
+    printf ("info: expecting success for %s%%%s\n", address, scope);
51f0aa
+  struct in6_addr addr = from_string (address);
51f0aa
+  uint32_t actual = expected + 1;
51f0aa
+  if (__inet6_scopeid_pton (&addr, scope, &actual) != 0)
51f0aa
+    {
51f0aa
+      support_record_failure ();
51f0aa
+      printf ("error: unexpected failure for %s%%%s\n",
51f0aa
+              address, scope);
51f0aa
+    }
51f0aa
+  if (actual != expected)
51f0aa
+    {
51f0aa
+      support_record_failure ();
51f0aa
+      printf ("error: unexpected result for for %s%%%s\n",
51f0aa
+              address, scope);
51f0aa
+      printf ("  expected: %" PRIu32 "\n", expected);
51f0aa
+      printf ("  actual:   %" PRIu32 "\n", actual);
51f0aa
+    }
51f0aa
+
51f0aa
+  struct sockaddr_in6 sa;
51f0aa
+  memset (&sa, 0xc0, sizeof (sa));
51f0aa
+  if (call_gai (AF_UNSPEC, address, scope, &sa))
51f0aa
+    check_ai ("AF_UNSPEC", address, scope, &sa, &addr, expected);
51f0aa
+  else
51f0aa
+    {
51f0aa
+      support_record_failure ();
51f0aa
+      printf ("error: unexpected getaddrinfo failure for %s%%%s (AF_UNSPEC)\n",
51f0aa
+              address, scope);
51f0aa
+    }
51f0aa
+  memset (&sa, 0xc0, sizeof (sa));
51f0aa
+  if (call_gai (AF_INET6, address, scope, &sa))
51f0aa
+    check_ai ("AF_INET6", address, scope, &sa, &addr, expected);
51f0aa
+  else
51f0aa
+    {
51f0aa
+      support_record_failure ();
51f0aa
+      printf ("error: unexpected getaddrinfo failure for %s%%%s (AF_INET6)\n",
51f0aa
+              address, scope);
51f0aa
+    }
51f0aa
+}
51f0aa
+
51f0aa
+static int
51f0aa
+do_test (void)
51f0aa
+{
51f0aa
+  setup_interface ();
51f0aa
+
51f0aa
+  static const char *test_addresses[]
51f0aa
+    = { "::", "::1", "2001:db8::1", NULL };
51f0aa
+  for (int i = 0; test_addresses[i] != NULL; ++i)
51f0aa
+    {
51f0aa
+      expect_success (test_addresses[i], "0", 0);
51f0aa
+      expect_success (test_addresses[i], "5555", 5555);
51f0aa
+
51f0aa
+      expect_failure (test_addresses[i], "");
51f0aa
+      expect_failure (test_addresses[i], "-1");
51f0aa
+      expect_failure (test_addresses[i], "-99");
51f0aa
+      expect_failure (test_addresses[i], "037777777777");
51f0aa
+      expect_failure (test_addresses[i], "0x");
51f0aa
+      expect_failure (test_addresses[i], "0x1");
51f0aa
+    }
51f0aa
+
51f0aa
+  if (interface_name != NULL)
51f0aa
+    {
51f0aa
+      expect_success ("fe80::1", interface_name, interface_index);
51f0aa
+      expect_success ("ff02::1", interface_name, interface_index);
51f0aa
+      expect_success ("ff01::1", interface_name, interface_index);
51f0aa
+      expect_failure ("::", interface_name);
51f0aa
+      expect_failure ("::1", interface_name);
51f0aa
+      expect_failure ("2001:db8::1", interface_name);
51f0aa
+    }
51f0aa
+
51f0aa
+  return 0;
51f0aa
+}
51f0aa
+
51f0aa
+#include <support/test-driver.c>
51f0aa
diff --git a/resolv/Makefile b/resolv/Makefile
51f0aa
index a86646d4ef61b77d..3f525bcc75ee83ae 100644
51f0aa
--- a/resolv/Makefile
51f0aa
+++ b/resolv/Makefile
51f0aa
@@ -29,7 +29,7 @@ headers	:= resolv.h \
51f0aa
 routines := herror inet_addr inet_ntop inet_pton nsap_addr res_init \
51f0aa
 	    res_hconf res_libc res-state
51f0aa
 
51f0aa
-tests = tst-aton tst-leaks tst-inet_ntop
51f0aa
+tests = tst-aton tst-leaks tst-inet_ntop tst-inet_pton
51f0aa
 xtests = tst-leaks2
51f0aa
 
51f0aa
 generate := mtrace-tst-leaks tst-leaks.mtrace tst-leaks2.mtrace
51f0aa
diff --git a/resolv/Versions b/resolv/Versions
51f0aa
index 152ef3f68f9a8b48..24b07ef770ed3915 100644
51f0aa
--- a/resolv/Versions
51f0aa
+++ b/resolv/Versions
51f0aa
@@ -29,6 +29,7 @@ libc {
51f0aa
     __h_errno; __resp;
51f0aa
 
51f0aa
     __res_maybe_init; __res_iclose;
51f0aa
+    __inet_pton_length;
51f0aa
   }
51f0aa
 }
51f0aa
 
51f0aa
diff --git a/resolv/inet_pton.c b/resolv/inet_pton.c
51f0aa
index c507013e4ea9a90b..16ee33e0c0dfb015 100644
51f0aa
--- a/resolv/inet_pton.c
51f0aa
+++ b/resolv/inet_pton.c
51f0aa
@@ -1,3 +1,20 @@
51f0aa
+/* Copyright (C) 1996-2017 Free Software Foundation, Inc.
51f0aa
+   This file is part of the GNU C Library.
51f0aa
+
51f0aa
+   The GNU C Library is free software; you can redistribute it and/or
51f0aa
+   modify it under the terms of the GNU Lesser General Public
51f0aa
+   License as published by the Free Software Foundation; either
51f0aa
+   version 2.1 of the License, or (at your option) any later version.
51f0aa
+
51f0aa
+   The GNU C Library is distributed in the hope that it will be useful,
51f0aa
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
51f0aa
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
51f0aa
+   Lesser General Public License for more details.
51f0aa
+
51f0aa
+   You should have received a copy of the GNU Lesser General Public
51f0aa
+   License along with the GNU C Library; if not, see
51f0aa
+   <http://www.gnu.org/licenses/>.  */
51f0aa
+
51f0aa
 /*
51f0aa
  * Copyright (c) 1996,1999 by Internet Software Consortium.
51f0aa
  *
51f0aa
@@ -15,207 +32,203 @@
51f0aa
  * SOFTWARE.
51f0aa
  */
51f0aa
 
51f0aa
-#if defined(LIBC_SCCS) && !defined(lint)
51f0aa
-static const char rcsid[] = "$BINDId: inet_pton.c,v 1.7 1999/10/13 16:39:28 vixie Exp $";
51f0aa
-#endif /* LIBC_SCCS and not lint */
51f0aa
-
51f0aa
-#include <sys/param.h>
51f0aa
-#include <sys/types.h>
51f0aa
-#include <sys/socket.h>
51f0aa
-#include <netinet/in.h>
51f0aa
 #include <arpa/inet.h>
51f0aa
 #include <arpa/nameser.h>
51f0aa
 #include <ctype.h>
51f0aa
-#include <string.h>
51f0aa
 #include <errno.h>
51f0aa
+#include <netinet/in.h>
51f0aa
+#include <resolv/resolv-internal.h>
51f0aa
+#include <string.h>
51f0aa
+#include <sys/socket.h>
51f0aa
+#include <sys/types.h>
51f0aa
 
51f0aa
-/*
51f0aa
- * WARNING: Don't even consider trying to compile this on a system where
51f0aa
- * sizeof(int) < 4.  sizeof(int) > 4 is fine; all the world's not a VAX.
51f0aa
- */
51f0aa
+static int inet_pton4 (const char *src, const char *src_end, u_char *dst);
51f0aa
+static int inet_pton6 (const char *src, const char *src_end, u_char *dst);
51f0aa
 
51f0aa
-static int inet_pton4 (const char *src, u_char *dst) internal_function;
51f0aa
-static int inet_pton6 (const char *src, u_char *dst) internal_function;
51f0aa
-
51f0aa
-/* int
51f0aa
- * inet_pton(af, src, dst)
51f0aa
- *	convert from presentation format (which usually means ASCII printable)
51f0aa
- *	to network format (which is usually some kind of binary format).
51f0aa
- * return:
51f0aa
- *	1 if the address was valid for the specified address family
51f0aa
- *	0 if the address wasn't valid (`dst' is untouched in this case)
51f0aa
- *	-1 if some other error occurred (`dst' is untouched in this case, too)
51f0aa
- * author:
51f0aa
- *	Paul Vixie, 1996.
51f0aa
- */
51f0aa
 int
51f0aa
-inet_pton(af, src, dst)
51f0aa
-	int af;
51f0aa
-	const char *src;
51f0aa
-	void *dst;
51f0aa
+__inet_pton_length (int af, const char *src, size_t srclen, void *dst)
51f0aa
 {
51f0aa
-	switch (af) {
51f0aa
-	case AF_INET:
51f0aa
-		return (inet_pton4(src, dst));
51f0aa
-	case AF_INET6:
51f0aa
-		return (inet_pton6(src, dst));
51f0aa
-	default:
51f0aa
-		__set_errno (EAFNOSUPPORT);
51f0aa
-		return (-1);
51f0aa
-	}
51f0aa
-	/* NOTREACHED */
51f0aa
+  switch (af)
51f0aa
+    {
51f0aa
+    case AF_INET:
51f0aa
+      return inet_pton4 (src, src + srclen, dst);
51f0aa
+    case AF_INET6:
51f0aa
+      return inet_pton6 (src, src + srclen, dst);
51f0aa
+    default:
51f0aa
+      __set_errno (EAFNOSUPPORT);
51f0aa
+      return -1;
51f0aa
+    }
51f0aa
 }
51f0aa
-libc_hidden_def (inet_pton)
51f0aa
-
51f0aa
-/* int
51f0aa
- * inet_pton4(src, dst)
51f0aa
- *	like inet_aton() but without all the hexadecimal, octal (with the
51f0aa
- *	exception of 0) and shorthand.
51f0aa
- * return:
51f0aa
- *	1 if `src' is a valid dotted quad, else 0.
51f0aa
- * notice:
51f0aa
- *	does not touch `dst' unless it's returning 1.
51f0aa
- * author:
51f0aa
- *	Paul Vixie, 1996.
51f0aa
- */
51f0aa
+libc_hidden_def (__inet_pton_length)
51f0aa
+
51f0aa
+/* Like __inet_pton_length, but use strlen (SRC) as the length of
51f0aa
+   SRC.  */
51f0aa
+int
51f0aa
+__inet_pton (int af, const char *src, void *dst)
51f0aa
+{
51f0aa
+  return __inet_pton_length (af, src, strlen (src), dst);
51f0aa
+}
51f0aa
+libc_hidden_def (__inet_pton)
51f0aa
+weak_alias (__inet_pton, inet_pton)
51f0aa
+libc_hidden_weak (inet_pton)
51f0aa
+
51f0aa
+/* Like inet_aton but without all the hexadecimal, octal and shorthand
51f0aa
+   (and trailing garbage is not ignored).  Return 1 if SRC is a valid
51f0aa
+   dotted quad, else 0.  This function does not touch DST unless it's
51f0aa
+   returning 1.
51f0aa
+   Author: Paul Vixie, 1996.  */
51f0aa
 static int
51f0aa
-internal_function
51f0aa
-inet_pton4(src, dst)
51f0aa
-	const char *src;
51f0aa
-	u_char *dst;
51f0aa
+inet_pton4 (const char *src, const char *end, unsigned char *dst)
51f0aa
 {
51f0aa
-	int saw_digit, octets, ch;
51f0aa
-	u_char tmp[NS_INADDRSZ], *tp;
51f0aa
-
51f0aa
-	saw_digit = 0;
51f0aa
-	octets = 0;
51f0aa
-	*(tp = tmp) = 0;
51f0aa
-	while ((ch = *src++) != '\0') {
51f0aa
-
51f0aa
-		if (ch >= '0' && ch <= '9') {
51f0aa
-			u_int new = *tp * 10 + (ch - '0');
51f0aa
-
51f0aa
-			if (saw_digit && *tp == 0)
51f0aa
-				return (0);
51f0aa
-			if (new > 255)
51f0aa
-				return (0);
51f0aa
-			*tp = new;
51f0aa
-			if (! saw_digit) {
51f0aa
-				if (++octets > 4)
51f0aa
-					return (0);
51f0aa
-				saw_digit = 1;
51f0aa
-			}
51f0aa
-		} else if (ch == '.' && saw_digit) {
51f0aa
-			if (octets == 4)
51f0aa
-				return (0);
51f0aa
-			*++tp = 0;
51f0aa
-			saw_digit = 0;
51f0aa
-		} else
51f0aa
-			return (0);
51f0aa
-	}
51f0aa
-	if (octets < 4)
51f0aa
-		return (0);
51f0aa
-	memcpy(dst, tmp, NS_INADDRSZ);
51f0aa
-	return (1);
51f0aa
+  int saw_digit, octets, ch;
51f0aa
+  unsigned char tmp[NS_INADDRSZ], *tp;
51f0aa
+
51f0aa
+  saw_digit = 0;
51f0aa
+  octets = 0;
51f0aa
+  *(tp = tmp) = 0;
51f0aa
+  while (src < end)
51f0aa
+    {
51f0aa
+      ch = *src++;
51f0aa
+      if (ch >= '0' && ch <= '9')
51f0aa
+        {
51f0aa
+          unsigned int new = *tp * 10 + (ch - '0');
51f0aa
+
51f0aa
+          if (saw_digit && *tp == 0)
51f0aa
+            return 0;
51f0aa
+          if (new > 255)
51f0aa
+            return 0;
51f0aa
+          *tp = new;
51f0aa
+          if (! saw_digit)
51f0aa
+            {
51f0aa
+              if (++octets > 4)
51f0aa
+                return 0;
51f0aa
+              saw_digit = 1;
51f0aa
+            }
51f0aa
+        }
51f0aa
+      else if (ch == '.' && saw_digit)
51f0aa
+        {
51f0aa
+          if (octets == 4)
51f0aa
+            return 0;
51f0aa
+          *++tp = 0;
51f0aa
+          saw_digit = 0;
51f0aa
+        }
51f0aa
+      else
51f0aa
+        return 0;
51f0aa
+    }
51f0aa
+  if (octets < 4)
51f0aa
+    return 0;
51f0aa
+  memcpy (dst, tmp, NS_INADDRSZ);
51f0aa
+  return 1;
51f0aa
 }
51f0aa
 
51f0aa
-/* int
51f0aa
- * inet_pton6(src, dst)
51f0aa
- *	convert presentation level address to network order binary form.
51f0aa
- * return:
51f0aa
- *	1 if `src' is a valid [RFC1884 2.2] address, else 0.
51f0aa
- * notice:
51f0aa
- *	(1) does not touch `dst' unless it's returning 1.
51f0aa
- *	(2) :: in a full address is silently ignored.
51f0aa
- * credit:
51f0aa
- *	inspired by Mark Andrews.
51f0aa
- * author:
51f0aa
- *	Paul Vixie, 1996.
51f0aa
- */
51f0aa
+/* Return the value of CH as a hexademical digit, or -1 if it is a
51f0aa
+   different type of character.  */
51f0aa
+static int
51f0aa
+hex_digit_value (char ch)
51f0aa
+{
51f0aa
+  if ('0' <= ch && ch <= '9')
51f0aa
+    return ch - '0';
51f0aa
+  if ('a' <= ch && ch <= 'f')
51f0aa
+    return ch - 'a' + 10;
51f0aa
+  if ('A' <= ch && ch <= 'F')
51f0aa
+    return ch - 'A' + 10;
51f0aa
+  return -1;
51f0aa
+}
51f0aa
+
51f0aa
+/* Convert presentation-level IPv6 address to network order binary
51f0aa
+   form.  Return 1 if SRC is a valid [RFC1884 2.2] address, else 0.
51f0aa
+   This function does not touch DST unless it's returning 1.
51f0aa
+   Author: Paul Vixie, 1996.  Inspired by Mark Andrews.  */
51f0aa
 static int
51f0aa
-internal_function
51f0aa
-inet_pton6(src, dst)
51f0aa
-	const char *src;
51f0aa
-	u_char *dst;
51f0aa
+inet_pton6 (const char *src, const char *src_endp, unsigned char *dst)
51f0aa
 {
51f0aa
-	static const char xdigits[] = "0123456789abcdef";
51f0aa
-	u_char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
51f0aa
-	const char *curtok;
51f0aa
-	int ch, saw_xdigit;
51f0aa
-	u_int val;
51f0aa
-
51f0aa
-	tp = memset(tmp, '\0', NS_IN6ADDRSZ);
51f0aa
-	endp = tp + NS_IN6ADDRSZ;
51f0aa
-	colonp = NULL;
51f0aa
-	/* Leading :: requires some special handling. */
51f0aa
-	if (*src == ':')
51f0aa
-		if (*++src != ':')
51f0aa
-			return (0);
51f0aa
-	curtok = src;
51f0aa
-	saw_xdigit = 0;
51f0aa
-	val = 0;
51f0aa
-	while ((ch = tolower (*src++)) != '\0') {
51f0aa
-		const char *pch;
51f0aa
-
51f0aa
-		pch = strchr(xdigits, ch);
51f0aa
-		if (pch != NULL) {
51f0aa
-			val <<= 4;
51f0aa
-			val |= (pch - xdigits);
51f0aa
-			if (val > 0xffff)
51f0aa
-				return (0);
51f0aa
-			saw_xdigit = 1;
51f0aa
-			continue;
51f0aa
-		}
51f0aa
-		if (ch == ':') {
51f0aa
-			curtok = src;
51f0aa
-			if (!saw_xdigit) {
51f0aa
-				if (colonp)
51f0aa
-					return (0);
51f0aa
-				colonp = tp;
51f0aa
-				continue;
51f0aa
-			} else if (*src == '\0') {
51f0aa
-				return (0);
51f0aa
-			}
51f0aa
-			if (tp + NS_INT16SZ > endp)
51f0aa
-				return (0);
51f0aa
-			*tp++ = (u_char) (val >> 8) & 0xff;
51f0aa
-			*tp++ = (u_char) val & 0xff;
51f0aa
-			saw_xdigit = 0;
51f0aa
-			val = 0;
51f0aa
-			continue;
51f0aa
-		}
51f0aa
-		if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
51f0aa
-		    inet_pton4(curtok, tp) > 0) {
51f0aa
-			tp += NS_INADDRSZ;
51f0aa
-			saw_xdigit = 0;
51f0aa
-			break;	/* '\0' was seen by inet_pton4(). */
51f0aa
-		}
51f0aa
-		return (0);
51f0aa
+  unsigned char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
51f0aa
+  const char *curtok;
51f0aa
+  int ch;
51f0aa
+  size_t xdigits_seen;	/* Number of hex digits since colon.  */
51f0aa
+  unsigned int val;
51f0aa
+
51f0aa
+  tp = memset (tmp, '\0', NS_IN6ADDRSZ);
51f0aa
+  endp = tp + NS_IN6ADDRSZ;
51f0aa
+  colonp = NULL;
51f0aa
+
51f0aa
+  /* Leading :: requires some special handling.  */
51f0aa
+  if (src == src_endp)
51f0aa
+    return 0;
51f0aa
+  if (*src == ':')
51f0aa
+    {
51f0aa
+      ++src;
51f0aa
+      if (src == src_endp || *src != ':')
51f0aa
+        return 0;
51f0aa
+    }
51f0aa
+
51f0aa
+  curtok = src;
51f0aa
+  xdigits_seen = 0;
51f0aa
+  val = 0;
51f0aa
+  while (src < src_endp)
51f0aa
+    {
51f0aa
+      ch = *src++;
51f0aa
+      int digit = hex_digit_value (ch);
51f0aa
+      if (digit >= 0)
51f0aa
+	{
51f0aa
+	  if (xdigits_seen == 4)
51f0aa
+	    return 0;
51f0aa
+	  val <<= 4;
51f0aa
+	  val |= digit;
51f0aa
+	  if (val > 0xffff)
51f0aa
+	    return 0;
51f0aa
+	  ++xdigits_seen;
51f0aa
+	  continue;
51f0aa
 	}
51f0aa
-	if (saw_xdigit) {
51f0aa
-		if (tp + NS_INT16SZ > endp)
51f0aa
-			return (0);
51f0aa
-		*tp++ = (u_char) (val >> 8) & 0xff;
51f0aa
-		*tp++ = (u_char) val & 0xff;
51f0aa
+      if (ch == ':')
51f0aa
+	{
51f0aa
+	  curtok = src;
51f0aa
+	  if (xdigits_seen == 0)
51f0aa
+	    {
51f0aa
+	      if (colonp)
51f0aa
+		return 0;
51f0aa
+	      colonp = tp;
51f0aa
+	      continue;
51f0aa
+	    }
51f0aa
+	  else if (src == src_endp)
51f0aa
+            return 0;
51f0aa
+	  if (tp + NS_INT16SZ > endp)
51f0aa
+	    return 0;
51f0aa
+	  *tp++ = (unsigned char) (val >> 8) & 0xff;
51f0aa
+	  *tp++ = (unsigned char) val & 0xff;
51f0aa
+	  xdigits_seen = 0;
51f0aa
+	  val = 0;
51f0aa
+	  continue;
51f0aa
 	}
51f0aa
-	if (colonp != NULL) {
51f0aa
-		/*
51f0aa
-		 * Since some memmove()'s erroneously fail to handle
51f0aa
-		 * overlapping regions, we'll do the shift by hand.
51f0aa
-		 */
51f0aa
-		const int n = tp - colonp;
51f0aa
-		int i;
51f0aa
-
51f0aa
-		if (tp == endp)
51f0aa
-			return (0);
51f0aa
-		for (i = 1; i <= n; i++) {
51f0aa
-			endp[- i] = colonp[n - i];
51f0aa
-			colonp[n - i] = 0;
51f0aa
-		}
51f0aa
-		tp = endp;
51f0aa
+      if (ch == '.' && ((tp + NS_INADDRSZ) <= endp)
51f0aa
+          && inet_pton4 (curtok, src_endp, tp) > 0)
51f0aa
+	{
51f0aa
+	  tp += NS_INADDRSZ;
51f0aa
+	  xdigits_seen = 0;
51f0aa
+	  break;  /* '\0' was seen by inet_pton4.  */
51f0aa
 	}
51f0aa
-	if (tp != endp)
51f0aa
-		return (0);
51f0aa
-	memcpy(dst, tmp, NS_IN6ADDRSZ);
51f0aa
-	return (1);
51f0aa
+      return 0;
51f0aa
+    }
51f0aa
+  if (xdigits_seen > 0)
51f0aa
+    {
51f0aa
+      if (tp + NS_INT16SZ > endp)
51f0aa
+	return 0;
51f0aa
+      *tp++ = (unsigned char) (val >> 8) & 0xff;
51f0aa
+      *tp++ = (unsigned char) val & 0xff;
51f0aa
+    }
51f0aa
+  if (colonp != NULL)
51f0aa
+    {
51f0aa
+      /* Replace :: with zeros.  */
51f0aa
+      if (tp == endp)
51f0aa
+        /* :: would expand to a zero-width field.  */
51f0aa
+        return 0;
51f0aa
+      size_t n = tp - colonp;
51f0aa
+      memmove (endp - n, colonp, n);
51f0aa
+      memset (colonp, 0, endp - n - colonp);
51f0aa
+      tp = endp;
51f0aa
+    }
51f0aa
+  if (tp != endp)
51f0aa
+    return 0;
51f0aa
+  memcpy (dst, tmp, NS_IN6ADDRSZ);
51f0aa
+  return 1;
51f0aa
 }
51f0aa
diff --git a/resolv/resolv-internal.h b/resolv/resolv-internal.h
51f0aa
index 269758c416544cb4..a9f5659dbbacb477 100644
51f0aa
--- a/resolv/resolv-internal.h
51f0aa
+++ b/resolv/resolv-internal.h
51f0aa
@@ -32,4 +32,13 @@ res_use_inet6 (void)
51f0aa
   return _res.options & DEPRECATED_RES_USE_INET6;
51f0aa
 }
51f0aa
 
51f0aa
+/* Convert from presentation format (which usually means ASCII
51f0aa
+   printable) to network format (which is usually some kind of binary
51f0aa
+   format).  The input is in the range [SRC, SRC + SRCLEN).  The
51f0aa
+   output is written to DST (which has to be 4 or 16 bytes long,
51f0aa
+   depending on AF).  Return 0 for invalid input, 1 for success, -1
51f0aa
+   for an invalid address family.  */
51f0aa
+int __inet_pton_length (int af, const char *src, size_t srclen, void *);
51f0aa
+libc_hidden_proto (__inet_pton_length)
51f0aa
+
51f0aa
 #endif  /* _RESOLV_INTERNAL_H */
51f0aa
diff --git a/resolv/tst-inet_pton.c b/resolv/tst-inet_pton.c
51f0aa
new file mode 100644
51f0aa
index 0000000000000000..4bb9f8119378b467
51f0aa
--- /dev/null
51f0aa
+++ b/resolv/tst-inet_pton.c
51f0aa
@@ -0,0 +1,537 @@
51f0aa
+/* Test inet_pton functions.
51f0aa
+   Copyright (C) 2017 Free Software Foundation, Inc.
51f0aa
+   This file is part of the GNU C Library.
51f0aa
+
51f0aa
+   The GNU C Library is free software; you can redistribute it and/or
51f0aa
+   modify it under the terms of the GNU Lesser General Public
51f0aa
+   License as published by the Free Software Foundation; either
51f0aa
+   version 2.1 of the License, or (at your option) any later version.
51f0aa
+
51f0aa
+   The GNU C Library is distributed in the hope that it will be useful,
51f0aa
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
51f0aa
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
51f0aa
+   Lesser General Public License for more details.
51f0aa
+
51f0aa
+   You should have received a copy of the GNU Lesser General Public
51f0aa
+   License along with the GNU C Library; if not, see
51f0aa
+   <http://www.gnu.org/licenses/>.  */
51f0aa
+
51f0aa
+#include <arpa/inet.h>
51f0aa
+#include <resolv/resolv-internal.h>
51f0aa
+#include <stdbool.h>
51f0aa
+#include <stdio.h>
51f0aa
+#include <string.h>
51f0aa
+#include <support/check.h>
51f0aa
+#include <support/xunistd.h>
51f0aa
+#include <sys/mman.h>
51f0aa
+#include <sys/param.h>
51f0aa
+#include <unistd.h>
51f0aa
+
51f0aa
+/* Memory region created by next_to_fault_allocate.  */
51f0aa
+struct next_to_fault
51f0aa
+{
51f0aa
+  /* The user data.  */
51f0aa
+  char *buffer;
51f0aa
+  size_t length;
51f0aa
+
51f0aa
+  /* The entire allocated region.  */
51f0aa
+  void *region_start;
51f0aa
+  size_t region_size;
51f0aa
+};
51f0aa
+
51f0aa
+/* Allocate a buffer of SIZE bytes just before a page which is mapped
51f0aa
+   with PROT_NONE (so that overrunning the buffer will cause a
51f0aa
+   fault).  */
51f0aa
+static struct next_to_fault
51f0aa
+next_to_fault_allocate (size_t size)
51f0aa
+{
51f0aa
+  long page_size = sysconf (_SC_PAGE_SIZE);
51f0aa
+  TEST_VERIFY_EXIT (page_size > 0);
51f0aa
+  struct next_to_fault result;
51f0aa
+  result.region_size = roundup (size, page_size) + page_size;
51f0aa
+  TEST_VERIFY_EXIT (size + page_size > size);
51f0aa
+  TEST_VERIFY_EXIT (result.region_size > size);
51f0aa
+  result.region_start
51f0aa
+    = xmmap (NULL, result.region_size, PROT_READ | PROT_WRITE,
51f0aa
+             MAP_PRIVATE | MAP_ANONYMOUS, -1);
51f0aa
+  /* Unmap the page after the allocation.  */
51f0aa
+  xmprotect (result.region_start + (result.region_size - page_size),
51f0aa
+             page_size, PROT_NONE);
51f0aa
+  /* Align the allocation within the region so that it ends just
51f0aa
+     before the PROT_NONE page.  */
51f0aa
+  result.buffer = result.region_start + result.region_size - page_size - size;
51f0aa
+  result.length = size;
51f0aa
+  return result;
51f0aa
+}
51f0aa
+
51f0aa
+/* Deallocate the memory region allocated by
51f0aa
+   next_to_fault_allocate.  */
51f0aa
+static void
51f0aa
+next_to_fault_free (struct next_to_fault *ntf)
51f0aa
+{
51f0aa
+  xmunmap (ntf->region_start, ntf->region_size);
51f0aa
+  *ntf = (struct next_to_fault) { NULL, };
51f0aa
+}
51f0aa
+
51f0aa
+struct test_case
51f0aa
+{
51f0aa
+  /* The input data.  */
51f0aa
+  const char *input;
51f0aa
+
51f0aa
+  /* True if AF_INET parses successfully.  */
51f0aa
+  bool ipv4_ok;
51f0aa
+
51f0aa
+  /* True if AF_INET6 parses successfully.  */
51f0aa
+  bool ipv6_ok;
51f0aa
+
51f0aa
+  /* Expected result for AF_INET.  */
51f0aa
+  unsigned char ipv4_expected[4];
51f0aa
+
51f0aa
+  /* Expected result for AF_INET6.  */
51f0aa
+  unsigned char ipv6_expected[16];
51f0aa
+};
51f0aa
+
51f0aa
+static void
51f0aa
+check_result (const char *what, const struct test_case *t, int family,
51f0aa
+              void *result_buffer, int inet_ret)
51f0aa
+{
51f0aa
+  TEST_VERIFY_EXIT (inet_ret >= -1);
51f0aa
+  TEST_VERIFY_EXIT (inet_ret <= 1);
51f0aa
+
51f0aa
+  int ok;
51f0aa
+  const unsigned char *expected;
51f0aa
+  size_t result_size;
51f0aa
+  switch (family)
51f0aa
+    {
51f0aa
+    case AF_INET:
51f0aa
+      ok = t->ipv4_ok;
51f0aa
+      expected = t->ipv4_expected;
51f0aa
+      result_size = 4;
51f0aa
+      break;
51f0aa
+    case AF_INET6:
51f0aa
+      ok = t->ipv6_ok;
51f0aa
+      expected = t->ipv6_expected;
51f0aa
+      result_size = 16;
51f0aa
+      break;
51f0aa
+    default:
51f0aa
+      FAIL_EXIT1 ("invalid address family %d", family);
51f0aa
+    }
51f0aa
+
51f0aa
+  if (inet_ret != ok)
51f0aa
+    {
51f0aa
+      support_record_failure ();
51f0aa
+      printf ("error: %s return value mismatch for [[%s]], family %d\n"
51f0aa
+              "  expected: %d\n"
51f0aa
+              "  actual: %d\n",
51f0aa
+              what, t->input, family, ok, inet_ret);
51f0aa
+      return;
51f0aa
+    }
51f0aa
+  if (memcmp (result_buffer, expected, result_size) != 0)
51f0aa
+    {
51f0aa
+      support_record_failure ();
51f0aa
+      printf ("error: %s result mismatch for [[%s]], family %d\n",
51f0aa
+              what, t->input, family);
51f0aa
+    }
51f0aa
+}
51f0aa
+
51f0aa
+static void
51f0aa
+run_one_test (const struct test_case *t)
51f0aa
+{
51f0aa
+  size_t test_len = strlen (t->input);
51f0aa
+
51f0aa
+  struct next_to_fault ntf_out4 = next_to_fault_allocate (4);
51f0aa
+  struct next_to_fault ntf_out6 = next_to_fault_allocate (16);
51f0aa
+
51f0aa
+  /* inet_pton requires NUL termination.  */
51f0aa
+  {
51f0aa
+    struct next_to_fault ntf_in = next_to_fault_allocate (test_len + 1);
51f0aa
+    memcpy (ntf_in.buffer, t->input, test_len + 1);
51f0aa
+    memset (ntf_out4.buffer, 0, 4);
51f0aa
+    check_result ("inet_pton", t, AF_INET, ntf_out4.buffer,
51f0aa
+                  inet_pton (AF_INET, ntf_in.buffer, ntf_out4.buffer));
51f0aa
+    memset (ntf_out6.buffer, 0, 16);
51f0aa
+    check_result ("inet_pton", t, AF_INET6, ntf_out6.buffer,
51f0aa
+                  inet_pton (AF_INET6, ntf_in.buffer, ntf_out6.buffer));
51f0aa
+    next_to_fault_free (&ntf_in);
51f0aa
+  }
51f0aa
+
51f0aa
+  /* __inet_pton_length does not require NUL termination.  */
51f0aa
+  {
51f0aa
+    struct next_to_fault ntf_in = next_to_fault_allocate (test_len);
51f0aa
+    memcpy (ntf_in.buffer, t->input, test_len);
51f0aa
+    memset (ntf_out4.buffer, 0, 4);
51f0aa
+    check_result ("__inet_pton_length", t, AF_INET, ntf_out4.buffer,
51f0aa
+                  __inet_pton_length (AF_INET, ntf_in.buffer, ntf_in.length,
51f0aa
+                                      ntf_out4.buffer));
51f0aa
+    memset (ntf_out6.buffer, 0, 16);
51f0aa
+    check_result ("__inet_pton_length", t, AF_INET6, ntf_out6.buffer,
51f0aa
+                  __inet_pton_length (AF_INET6, ntf_in.buffer, ntf_in.length,
51f0aa
+                                      ntf_out6.buffer));
51f0aa
+    next_to_fault_free (&ntf_in);
51f0aa
+  }
51f0aa
+
51f0aa
+  next_to_fault_free (&ntf_out4);
51f0aa
+  next_to_fault_free (&ntf_out6);
51f0aa
+}
51f0aa
+
51f0aa
+/* The test cases were manually crafted and the set enhanced with
51f0aa
+   American Fuzzy Lop.  */
51f0aa
+const struct test_case test_cases[] =
51f0aa
+  {
51f0aa
+    {.input = ".:", },
51f0aa
+    {.input = "0.0.0.0",
51f0aa
+     .ipv4_ok = true,
51f0aa
+     .ipv4_expected = {0, 0, 0, 0},
51f0aa
+    },
51f0aa
+    {.input = "0.:", },
51f0aa
+    {.input = "00", },
51f0aa
+    {.input = "0000000", },
51f0aa
+    {.input = "00000000000000000", },
51f0aa
+    {.input = "092.", },
51f0aa
+    {.input = "10.0.301.2", },
51f0aa
+    {.input = "127.0.0.1",
51f0aa
+     .ipv4_ok = true,
51f0aa
+     .ipv4_expected = {127, 0, 0, 1},
51f0aa
+    },
51f0aa
+    {.input = "19..", },
51f0aa
+    {.input = "192.0.2.-1", },
51f0aa
+    {.input = "192.0.2.01", },
51f0aa
+    {.input = "192.0.2.1.", },
51f0aa
+    {.input = "192.0.2.1192.", },
51f0aa
+    {.input = "192.0.2.192.\377..", },
51f0aa
+    {.input = "192.0.2.256", },
51f0aa
+    {.input = "192.0.2.27",
51f0aa
+     .ipv4_ok = true,
51f0aa
+     .ipv4_expected = {192, 0, 2, 27},
51f0aa
+    },
51f0aa
+    {.input = "192.0.201.", },
51f0aa
+    {.input = "192.0.261.", },
51f0aa
+    {.input = "192.0.2\256", },
51f0aa
+    {.input = "192.0.\262.", },
51f0aa
+    {.input = "192.062.", },
51f0aa
+    {.input = "192.092.\256", },
51f0aa
+    {.input = "192.0\2562.", },
51f0aa
+    {.input = "192.192.0.2661\031", },
51f0aa
+    {.input = "192.192.00n2.1.", },
51f0aa
+    {.input = "192.192.2.190.", },
51f0aa
+    {.input = "192.255.255.2555", },
51f0aa
+    {.input = "192.92.219\023.", },
51f0aa
+    {.input = "192.\260.2.", },
51f0aa
+    {.input = "1:1::1:1",
51f0aa
+     .ipv6_ok = true,
51f0aa
+     .ipv6_expected = {
51f0aa
+       0x0, 0x1, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0,
51f0aa
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x1
51f0aa
+     },
51f0aa
+    },
51f0aa
+    {.input = "2", },
51f0aa
+    {.input = "2.", },
51f0aa
+    {.input = "2001:db8:00001::f", },
51f0aa
+    {.input = "2001:db8:10000::f", },
51f0aa
+    {.input = "2001:db8:1234:5678:abcd:ef01:2345:67",
51f0aa
+     .ipv6_ok = true,
51f0aa
+     .ipv6_expected = {
51f0aa
+       0x20, 0x1, 0xd, 0xb8, 0x12, 0x34, 0x56, 0x78,
51f0aa
+       0xab, 0xcd, 0xef, 0x1, 0x23, 0x45, 0x0, 0x67
51f0aa
+     },
51f0aa
+    },
51f0aa
+    {.input = "2001:db8:1234:5678:abcd:ef01:2345:6789:1", },
51f0aa
+    {.input = "2001:db8:1234:5678:abcd:ef01:2345::6789", },
51f0aa
+    {.input = "2001:db8::0",
51f0aa
+     .ipv6_ok = true,
51f0aa
+     .ipv6_expected = {
51f0aa
+       0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
51f0aa
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0
51f0aa
+     },
51f0aa
+    },
51f0aa
+    {.input = "2001:db8::00",
51f0aa
+     .ipv6_ok = true,
51f0aa
+     .ipv6_expected = {
51f0aa
+       0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
51f0aa
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0
51f0aa
+     },
51f0aa
+    },
51f0aa
+    {.input = "2001:db8::1",
51f0aa
+     .ipv6_ok = true,
51f0aa
+     .ipv6_expected = {
51f0aa
+       0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
51f0aa
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1
51f0aa
+     },
51f0aa
+    },
51f0aa
+    {.input = "2001:db8::10",
51f0aa
+     .ipv6_ok = true,
51f0aa
+     .ipv6_expected = {
51f0aa
+       0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
51f0aa
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x10
51f0aa
+     },
51f0aa
+    },
51f0aa
+    {.input = "2001:db8::19",
51f0aa
+     .ipv6_ok = true,
51f0aa
+     .ipv6_expected = {
51f0aa
+       0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
51f0aa
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x19
51f0aa
+     },
51f0aa
+    },
51f0aa
+    {.input = "2001:db8::1::\012", },
51f0aa
+    {.input = "2001:db8::1::2\012", },
51f0aa
+    {.input = "2001:db8::2",
51f0aa
+     .ipv6_ok = true,
51f0aa
+     .ipv6_expected = {
51f0aa
+       0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
51f0aa
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2
51f0aa
+     },
51f0aa
+    },
51f0aa
+    {.input = "2001:db8::3",
51f0aa
+     .ipv6_ok = true,
51f0aa
+     .ipv6_expected = {
51f0aa
+       0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
51f0aa
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x3
51f0aa
+     },
51f0aa
+    },
51f0aa
+    {.input = "2001:db8::4",
51f0aa
+     .ipv6_ok = true,
51f0aa
+     .ipv6_expected = {
51f0aa
+       0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
51f0aa
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x4
51f0aa
+     },
51f0aa
+    },
51f0aa
+    {.input = "2001:db8::5",
51f0aa
+     .ipv6_ok = true,
51f0aa
+     .ipv6_expected = {
51f0aa
+       0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
51f0aa
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x5
51f0aa
+     },
51f0aa
+    },
51f0aa
+    {.input = "2001:db8::6",
51f0aa
+     .ipv6_ok = true,
51f0aa
+     .ipv6_expected = {
51f0aa
+       0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
51f0aa
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x6
51f0aa
+     },
51f0aa
+    },
51f0aa
+    {.input = "2001:db8::7",
51f0aa
+     .ipv6_ok = true,
51f0aa
+     .ipv6_expected = {
51f0aa
+       0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
51f0aa
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x7
51f0aa
+     },
51f0aa
+    },
51f0aa
+    {.input = "2001:db8::8",
51f0aa
+     .ipv6_ok = true,
51f0aa
+     .ipv6_expected = {
51f0aa
+       0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
51f0aa
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x8
51f0aa
+     },
51f0aa
+    },
51f0aa
+    {.input = "2001:db8::9",
51f0aa
+     .ipv6_ok = true,
51f0aa
+     .ipv6_expected = {
51f0aa
+       0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
51f0aa
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x9
51f0aa
+     },
51f0aa
+    },
51f0aa
+    {.input = "2001:db8::A",
51f0aa
+     .ipv6_ok = true,
51f0aa
+     .ipv6_expected = {
51f0aa
+       0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
51f0aa
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xa
51f0aa
+     },
51f0aa
+    },
51f0aa
+    {.input = "2001:db8::B",
51f0aa
+     .ipv6_ok = true,
51f0aa
+     .ipv6_expected = {
51f0aa
+       0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
51f0aa
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xb
51f0aa
+     },
51f0aa
+    },
51f0aa
+    {.input = "2001:db8::C",
51f0aa
+     .ipv6_ok = true,
51f0aa
+     .ipv6_expected = {
51f0aa
+       0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
51f0aa
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc
51f0aa
+     },
51f0aa
+    },
51f0aa
+    {.input = "2001:db8::D",
51f0aa
+     .ipv6_ok = true,
51f0aa
+     .ipv6_expected = {
51f0aa
+       0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
51f0aa
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xd
51f0aa
+     },
51f0aa
+    },
51f0aa
+    {.input = "2001:db8::E",
51f0aa
+     .ipv6_ok = true,
51f0aa
+     .ipv6_expected = {
51f0aa
+       0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
51f0aa
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xe
51f0aa
+     },
51f0aa
+    },
51f0aa
+    {.input = "2001:db8::F",
51f0aa
+     .ipv6_ok = true,
51f0aa
+     .ipv6_expected = {
51f0aa
+       0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
51f0aa
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf
51f0aa
+     },
51f0aa
+    },
51f0aa
+    {.input = "2001:db8::a",
51f0aa
+     .ipv6_ok = true,
51f0aa
+     .ipv6_expected = {
51f0aa
+       0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
51f0aa
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xa
51f0aa
+     },
51f0aa
+    },
51f0aa
+    {.input = "2001:db8::b",
51f0aa
+     .ipv6_ok = true,
51f0aa
+     .ipv6_expected = {
51f0aa
+       0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
51f0aa
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xb
51f0aa
+     },
51f0aa
+    },
51f0aa
+    {.input = "2001:db8::c",
51f0aa
+     .ipv6_ok = true,
51f0aa
+     .ipv6_expected = {
51f0aa
+       0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
51f0aa
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc
51f0aa
+     },
51f0aa
+    },
51f0aa
+    {.input = "2001:db8::d",
51f0aa
+     .ipv6_ok = true,
51f0aa
+     .ipv6_expected = {
51f0aa
+       0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
51f0aa
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xd
51f0aa
+     },
51f0aa
+    },
51f0aa
+    {.input = "2001:db8::e",
51f0aa
+     .ipv6_ok = true,
51f0aa
+     .ipv6_expected = {
51f0aa
+       0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
51f0aa
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xe
51f0aa
+     },
51f0aa
+    },
51f0aa
+    {.input = "2001:db8::f",
51f0aa
+     .ipv6_ok = true,
51f0aa
+     .ipv6_expected = {
51f0aa
+       0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
51f0aa
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf
51f0aa
+     },
51f0aa
+    },
51f0aa
+    {.input = "2001:db8::ff",
51f0aa
+     .ipv6_ok = true,
51f0aa
+     .ipv6_expected = {
51f0aa
+       0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
51f0aa
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xff
51f0aa
+     },
51f0aa
+    },
51f0aa
+    {.input = "2001:db8::ffff:2\012", },
51f0aa
+    {.input = "22", },
51f0aa
+    {.input = "2222@", },
51f0aa
+    {.input = "255.255.255.255",
51f0aa
+     .ipv4_ok = true,
51f0aa
+     .ipv4_expected = {255, 255, 255, 255},
51f0aa
+    },
51f0aa
+    {.input = "255.255.255.255\001", },
51f0aa
+    {.input = "255.255.255.25555", },
51f0aa
+    {.input = "2:", },
51f0aa
+    {.input = "2:a:8:EEEE::EEEE:F:EEE8:EEEE\034*:", },
51f0aa
+    {.input = "2:ff:1:1:7:ff:1:1:7.", },
51f0aa
+    {.input = "2f:0000000000000000000000000000000000000000000000000000000000"
51f0aa
+     "0000000000000000000000000000000000000000000000000000000000000000000000"
51f0aa
+     "0G01",
51f0aa
+    },
51f0aa
+    {.input = "429495", },
51f0aa
+    {.input = "5::5::", },
51f0aa
+    {.input = "6.6.", },
51f0aa
+    {.input = "992.", },
51f0aa
+    {.input = "::",
51f0aa
+     .ipv6_ok = true,
51f0aa
+     .ipv6_expected = {
51f0aa
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
51f0aa
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0
51f0aa
+     },
51f0aa
+    },
51f0aa
+    {.input = "::00001", },
51f0aa
+    {.input = "::1",
51f0aa
+     .ipv6_ok = true,
51f0aa
+     .ipv6_expected = {
51f0aa
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
51f0aa
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1
51f0aa
+     },
51f0aa
+    },
51f0aa
+    {.input = "::10000", },
51f0aa
+    {.input = "::1:1",
51f0aa
+     .ipv6_ok = true,
51f0aa
+     .ipv6_expected = {
51f0aa
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
51f0aa
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x1
51f0aa
+     },
51f0aa
+    },
51f0aa
+    {.input = "::ff:1:1:7.0.0.1",
51f0aa
+     .ipv6_ok = true,
51f0aa
+     .ipv6_expected = {
51f0aa
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xff,
51f0aa
+       0x0, 0x1, 0x0, 0x1, 0x7, 0x0, 0x0, 0x1
51f0aa
+     },
51f0aa
+    },
51f0aa
+    {.input = "::ff:1:1:7:ff:1:1:7.", },
51f0aa
+    {.input = "::ff:1:1:7ff:1:8:7.0.0.1", },
51f0aa
+    {.input = "::ff:1:1:7ff:1:8f:1:1:71", },
51f0aa
+    {.input = "::ffff:02fff:127.0.S1", },
51f0aa
+    {.input = "::ffff:127.0.0.1",
51f0aa
+     .ipv6_ok = true,
51f0aa
+     .ipv6_expected = {
51f0aa
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
51f0aa
+       0x0, 0x0, 0xff, 0xff, 0x7f, 0x0, 0x0, 0x1
51f0aa
+     },
51f0aa
+    },
51f0aa
+    {.input = "::ffff:1:7.0.0.1",
51f0aa
+     .ipv6_ok = true,
51f0aa
+     .ipv6_expected = {
51f0aa
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
51f0aa
+       0xff, 0xff, 0x0, 0x1, 0x7, 0x0, 0x0, 0x1
51f0aa
+     },
51f0aa
+    },
51f0aa
+    {.input = ":\272", },
51f0aa
+    {.input = "A:f:ff:1:1:D:ff:1:1::7.", },
51f0aa
+    {.input = "AAAAA.", },
51f0aa
+    {.input = "D:::", },
51f0aa
+    {.input = "DF8F", },
51f0aa
+    {.input = "F::",
51f0aa
+     .ipv6_ok = true,
51f0aa
+     .ipv6_expected = {
51f0aa
+       0x0, 0xf, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
51f0aa
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0
51f0aa
+     },
51f0aa
+    },
51f0aa
+    {.input = "F:A:8:EEEE:8:EEEE\034*:", },
51f0aa
+    {.input = "F:a:8:EEEE:8:EEEE\034*:", },
51f0aa
+    {.input = "F:ff:100:7ff:1:8:7.0.10.1",
51f0aa
+     .ipv6_ok = true,
51f0aa
+     .ipv6_expected = {
51f0aa
+       0x0, 0xf, 0x0, 0xff, 0x1, 0x0, 0x7, 0xff,
51f0aa
+       0x0, 0x1, 0x0, 0x8, 0x7, 0x0, 0xa, 0x1
51f0aa
+     },
51f0aa
+    },
51f0aa
+    {.input = "d92.", },
51f0aa
+    {.input = "ff:00000000000000000000000000000000000000000000000000000000000"
51f0aa
+     "00000000000000000000000000000000000000000000000000000000000000000001",
51f0aa
+    },
51f0aa
+    {.input = "fff2:2::ff2:2:f7",
51f0aa
+     .ipv6_ok = true,
51f0aa
+     .ipv6_expected = {
51f0aa
+       0xff, 0xf2, 0x0, 0x2, 0x0, 0x0, 0x0, 0x0,
51f0aa
+       0x0, 0x0, 0xf, 0xf2, 0x0, 0x2, 0x0, 0xf7
51f0aa
+     },
51f0aa
+    },
51f0aa
+    {.input = "ffff:ff:ff:fff:ff:ff:ff:", },
51f0aa
+    {.input = "\272:", },
51f0aa
+    {NULL}
51f0aa
+  };
51f0aa
+
51f0aa
+static int
51f0aa
+do_test (void)
51f0aa
+{
51f0aa
+  for (size_t i = 0; test_cases[i].input != NULL; ++i)
51f0aa
+    run_one_test (test_cases + i);
51f0aa
+  return 0;
51f0aa
+}
51f0aa
+
51f0aa
+#include <support/test-driver.c>
51f0aa
diff --git a/sysdeps/unix/sysv/linux/if_index.c b/sysdeps/unix/sysv/linux/if_index.c
51f0aa
index cf336839259b24b6..8ba5eae7818b49cd 100644
51f0aa
--- a/sysdeps/unix/sysv/linux/if_index.c
51f0aa
+++ b/sysdeps/unix/sysv/linux/if_index.c
51f0aa
@@ -1,4 +1,4 @@
51f0aa
-/* Copyright (C) 1997-2012 Free Software Foundation, Inc.
51f0aa
+/* Copyright (C) 1997-2017 Free Software Foundation, Inc.
51f0aa
    This file is part of the GNU C Library.
51f0aa
 
51f0aa
    The GNU C Library is free software; you can redistribute it and/or
51f0aa
@@ -24,15 +24,14 @@
51f0aa
 #include <net/if.h>
51f0aa
 #include <sys/socket.h>
51f0aa
 #include <sys/ioctl.h>
51f0aa
-#include <bits/libc-lock.h>
51f0aa
+#include <libc-lock.h>
51f0aa
 #include <not-cancel.h>
51f0aa
-#include <kernel-features.h>
51f0aa
 
51f0aa
 #include "netlinkaccess.h"
51f0aa
 
51f0aa
 
51f0aa
 unsigned int
51f0aa
-if_nametoindex (const char *ifname)
51f0aa
+__if_nametoindex (const char *ifname)
51f0aa
 {
51f0aa
 #ifndef SIOCGIFINDEX
51f0aa
   __set_errno (ENOSYS);
51f0aa
@@ -57,11 +56,13 @@ if_nametoindex (const char *ifname)
51f0aa
   return ifr.ifr_ifindex;
51f0aa
 #endif
51f0aa
 }
51f0aa
-libc_hidden_def (if_nametoindex)
51f0aa
+libc_hidden_def (__if_nametoindex)
51f0aa
+weak_alias (__if_nametoindex, if_nametoindex)
51f0aa
+libc_hidden_weak (if_nametoindex)
51f0aa
 
51f0aa
 
51f0aa
 void
51f0aa
-if_freenameindex (struct if_nameindex *ifn)
51f0aa
+__if_freenameindex (struct if_nameindex *ifn)
51f0aa
 {
51f0aa
   struct if_nameindex *ptr = ifn;
51f0aa
   while (ptr->if_name || ptr->if_index)
51f0aa
@@ -71,7 +72,9 @@ if_freenameindex (struct if_nameindex *ifn)
51f0aa
     }
51f0aa
   free (ifn);
51f0aa
 }
51f0aa
-libc_hidden_def (if_freenameindex)
51f0aa
+libc_hidden_def (__if_freenameindex)
51f0aa
+weak_alias (__if_freenameindex, if_freenameindex)
51f0aa
+libc_hidden_weak (if_freenameindex)
51f0aa
 
51f0aa
 
51f0aa
 static struct if_nameindex *
51f0aa
@@ -163,7 +166,7 @@ if_nameindex_netlink (void)
51f0aa
 		      if (idx[nifs].if_name == NULL)
51f0aa
 			{
51f0aa
 			  idx[nifs].if_index = 0;
51f0aa
-			  if_freenameindex (idx);
51f0aa
+			  __if_freenameindex (idx);
51f0aa
 			  idx = NULL;
51f0aa
 			  goto nomem;
51f0aa
 			}
51f0aa
@@ -190,7 +193,7 @@ if_nameindex_netlink (void)
51f0aa
 
51f0aa
 
51f0aa
 struct if_nameindex *
51f0aa
-if_nameindex (void)
51f0aa
+__if_nameindex (void)
51f0aa
 {
51f0aa
 #ifndef SIOCGIFINDEX
51f0aa
   __set_errno (ENOSYS);
51f0aa
@@ -200,11 +203,12 @@ if_nameindex (void)
51f0aa
   return result;
51f0aa
 #endif
51f0aa
 }
51f0aa
-libc_hidden_def (if_nameindex)
51f0aa
+weak_alias (__if_nameindex, if_nameindex)
51f0aa
+libc_hidden_weak (if_nameindex)
51f0aa
 
51f0aa
 
51f0aa
 char *
51f0aa
-if_indextoname (unsigned int ifindex, char *ifname)
51f0aa
+__if_indextoname (unsigned int ifindex, char *ifname)
51f0aa
 {
51f0aa
   /* We may be able to do the conversion directly, rather than searching a
51f0aa
      list.  This ioctl is not present in kernels before version 2.1.50.  */
51f0aa
@@ -233,4 +237,5 @@ if_indextoname (unsigned int ifindex, char *ifname)
51f0aa
   else
51f0aa
     return strncpy (ifname, ifr.ifr_name, IFNAMSIZ);
51f0aa
 }
51f0aa
-libc_hidden_def (if_indextoname)
51f0aa
+weak_alias (__if_indextoname, if_indextoname)
51f0aa
+libc_hidden_weak (if_indextoname)