]> sourceware.org Git - glibc.git/commitdiff
* nss/bug-erange.c: New file.
authorRoland McGrath <roland@gnu.org>
Thu, 12 Sep 2002 01:44:33 +0000 (01:44 +0000)
committerRoland McGrath <roland@gnu.org>
Thu, 12 Sep 2002 01:44:33 +0000 (01:44 +0000)
* nss/Makefile (tests): Add it.

ChangeLog
nss/Makefile
nss/bug-erange.c [new file with mode: 0644]

index b3f743635ce769fcdb462c6f837a733d1c0b7d52..688e0d52bf855333d56bf26969ab75893da53937 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,8 @@
 2002-09-11  Roland McGrath  <roland@redhat.com>
 
+       * nss/bug-erange.c: New file.
+       * nss/Makefile (tests): Add it.
+
        * nss/getXXbyYY_r.c (INTERNAL (REENTRANT_NAME)): Return errno
        instead of always EAGAIN when status is NSS_STATUS_TRYAGAIN.
 
index b81bdb44333145387567e43703f3d96914348686..1a244821961a7d9ecd181949832e9dc1762e1bb4 100644 (file)
@@ -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 (file)
index 0000000..5e535a0
--- /dev/null
@@ -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;
+}
This page took 0.051985 seconds and 5 git commands to generate.