summary refs log tree commit diff
path: root/nss
diff options
context:
space:
mode:
authorRoland McGrath <roland@gnu.org>2002-09-12 01:44:33 +0000
committerRoland McGrath <roland@gnu.org>2002-09-12 01:44:33 +0000
commit049e7c97d6f4d191717b06ede1da40965f032148 (patch)
tree96d6473964a863a991e02f2ffc0dc026b555620a /nss
parentf8494ee354855b4f6ab4a3ba31380c847177cbc0 (diff)
downloadglibc-049e7c97d6f4d191717b06ede1da40965f032148.tar.gz
glibc-049e7c97d6f4d191717b06ede1da40965f032148.tar.xz
glibc-049e7c97d6f4d191717b06ede1da40965f032148.zip
* nss/bug-erange.c: New file.
	* nss/Makefile (tests): Add it.
Diffstat (limited to 'nss')
-rw-r--r--nss/Makefile2
-rw-r--r--nss/bug-erange.c44
2 files changed, 45 insertions, 1 deletions
diff --git a/nss/Makefile b/nss/Makefile
index b81bdb4433..1a24482196 100644
--- a/nss/Makefile
+++ b/nss/Makefile
@@ -39,7 +39,7 @@ databases		= proto service hosts network grp pwd rpc ethers \
 others                  := getent
 install-bin             := getent
 
-tests			= test-netdb
+tests			= test-netdb bug-erange
 
 include ../Makeconfig
 
diff --git a/nss/bug-erange.c b/nss/bug-erange.c
new file mode 100644
index 0000000000..5e535a0fe4
--- /dev/null
+++ b/nss/bug-erange.c
@@ -0,0 +1,44 @@
+/* Test case for gethostbyname_r bug when buffer expansion required.  */
+
+#include <netdb.h>
+#include <arpa/inet.h>
+#include <errno.h>
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+int
+main (void)
+{
+  const char *host = "www.gnu.org";
+
+  /* This code approximates the example code in the library manual.  */
+
+  struct hostent hostbuf, *hp;
+  size_t hstbuflen;
+  char *tmphstbuf;
+  int res;
+  int herr;
+
+  hstbuflen = 16;		/* Make it way small to ensure ERANGE.  */
+  /* Allocate buffer, remember to free it to avoid memory leakage.  */
+  tmphstbuf = malloc (hstbuflen);
+
+  while ((res = gethostbyname_r (host, &hostbuf, tmphstbuf, hstbuflen,
+                                 &hp, &herr)) == ERANGE)
+    {
+      /* Enlarge the buffer.  */
+      hstbuflen *= 2;
+      tmphstbuf = realloc (tmphstbuf, hstbuflen);
+    }
+
+  if (res != 0 || hp == NULL)
+    {
+      printf ("gethostbyname_r failed: %s (errno: %m)\n", strerror (res));
+      return 1;
+    }
+
+  printf ("Got: %s %s\n", hp->h_name,
+	  inet_ntoa (*(struct in_addr *) hp->h_addr));
+  return 0;
+}