File: bug-erange.c

package info (click to toggle)
glibc 2.19-18+deb8u7
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 204,748 kB
  • sloc: ansic: 970,427; asm: 241,207; sh: 10,069; makefile: 8,476; cpp: 3,595; perl: 2,077; pascal: 1,839; awk: 1,704; yacc: 317; sed: 73
file content (52 lines) | stat: -rw-r--r-- 1,188 bytes parent folder | download | duplicates (31)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/* 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>
#include <unistd.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));

      if (access ("/etc/resolv.conf", R_OK))
	{
	  puts ("DNS probably not set up");
	  return 0;
	}

      return 1;
    }

  printf ("Got: %s %s\n", hp->h_name,
	  inet_ntoa (*(struct in_addr *) hp->h_addr));
  return 0;
}