[BZ #2013]
[glibc.git] / nss / bug-erange.c
blobb709418b5cff18e44c3515a2c9678cab8dd4cb0b
1 /* Test case for gethostbyname_r bug when buffer expansion required. */
3 #include <netdb.h>
4 #include <arpa/inet.h>
5 #include <errno.h>
6 #include <string.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <unistd.h>
11 int
12 main (void)
14 const char *host = "www.gnu.org";
16 /* This code approximates the example code in the library manual. */
18 struct hostent hostbuf, *hp;
19 size_t hstbuflen;
20 char *tmphstbuf;
21 int res;
22 int herr;
24 hstbuflen = 16; /* Make it way small to ensure ERANGE. */
25 /* Allocate buffer, remember to free it to avoid memory leakage. */
26 tmphstbuf = malloc (hstbuflen);
28 while ((res = gethostbyname_r (host, &hostbuf, tmphstbuf, hstbuflen,
29 &hp, &herr)) == ERANGE)
31 /* Enlarge the buffer. */
32 hstbuflen *= 2;
33 tmphstbuf = realloc (tmphstbuf, hstbuflen);
36 if (res != 0 || hp == NULL)
38 printf ("gethostbyname_r failed: %s (errno: %m)\n", strerror (res));
40 if (access ("/etc/resolv.conf", R_OK))
42 puts ("DNS probably not set up");
43 return 0;
46 return 1;
49 printf ("Got: %s %s\n", hp->h_name,
50 inet_ntoa (*(struct in_addr *) hp->h_addr));
51 return 0;