better checks to avoid free(NULL)
[platform/upstream/c-ares.git] / ares__get_hostent.c
1 /* Copyright 1998 by the Massachusetts Institute of Technology.
2  *
3  * Permission to use, copy, modify, and distribute this
4  * software and its documentation for any purpose and without
5  * fee is hereby granted, provided that the above copyright
6  * notice appear in all copies and that both that copyright
7  * notice and this permission notice appear in supporting
8  * documentation, and that the name of M.I.T. not be used in
9  * advertising or publicity pertaining to distribution of the
10  * software without specific, written prior permission.
11  * M.I.T. makes no representations about the suitability of
12  * this software for any purpose.  It is provided "as is"
13  * without express or implied warranty.
14  */
15
16 #include <sys/types.h>
17
18 #ifdef WIN32
19
20 #else
21 #include <sys/socket.h>
22 #include <netinet/in.h>
23 #include <arpa/inet.h>
24 #include <netdb.h>
25 #endif
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <ctype.h>
31
32 #include "ares.h"
33 #include "ares_private.h"
34
35 int ares__get_hostent(FILE *fp, struct hostent **host)
36 {
37   char *line = NULL, *p, *q, *canonical, **alias;
38   int status, linesize, end_at_hostname, naliases;
39   struct in_addr addr;
40   struct hostent *hostent = NULL;
41
42   while ((status = ares__read_line(fp, &line, &linesize)) == ARES_SUCCESS)
43     {
44       /* Skip comment lines; terminate line at comment character. */
45       if (*line == '#' || !*line)
46         continue;
47       p = strchr(line, '#');
48       if (p)
49         *p = 0;
50
51       /* Get the address part. */
52       p = line;
53       while (*p && !isspace((unsigned char)*p))
54         p++;
55       if (!*p)
56         continue;
57       *p = 0;
58       addr.s_addr = inet_addr(line);
59       if (addr.s_addr == INADDR_NONE)
60         continue;
61
62       /* Get the canonical hostname. */
63       p++;
64       while (isspace((unsigned char)*p))
65         p++;
66       if (!*p)
67         continue;
68       q = p;
69       while (*q && !isspace((unsigned char)*q))
70         q++;
71       end_at_hostname = (*q == 0);
72       *q = 0;
73       canonical = p;
74
75       naliases = 0;
76       if (!end_at_hostname)
77         {
78           /* Count the aliases. */
79           p = q + 1;
80           while (isspace((unsigned char)*p))
81             p++;
82           while (*p)
83             {
84               while (*p && !isspace((unsigned char)*p))
85                 p++;
86               while (isspace((unsigned char)*p))
87                 p++;
88               naliases++;
89             }
90         }
91
92       /* Allocate memory for the host structure. */
93       hostent = malloc(sizeof(struct hostent));
94       if (!hostent)
95         break;
96       hostent->h_aliases = NULL;
97       hostent->h_addr_list = NULL;
98       hostent->h_name = strdup(canonical);
99       if (!hostent->h_name)
100         break;
101       hostent->h_addr_list = malloc(2 * sizeof(char *));
102       if (!hostent->h_addr_list)
103         break;
104       hostent->h_addr_list[0] = malloc(sizeof(struct in_addr));
105       if (!hostent->h_addr_list[0])
106         break;
107       hostent->h_aliases = malloc((naliases + 1) * sizeof(char *));
108       if (!hostent->h_aliases)
109         break;
110
111       /* Copy in aliases. */
112       naliases = 0;
113       if (!end_at_hostname)
114         {
115           p = canonical + strlen(canonical) + 1;
116           while (isspace((unsigned char)*p))
117             p++;
118           while (*p)
119             {
120               q = p;
121               while (*q && !isspace((unsigned char)*q))
122                 q++;
123               hostent->h_aliases[naliases] = malloc(q - p + 1);
124               if (hostent->h_aliases[naliases] == NULL)
125                 break;
126               memcpy(hostent->h_aliases[naliases], p, q - p);
127               hostent->h_aliases[naliases][q - p] = 0;
128               p = q;
129               while (isspace((unsigned char)*p))
130                 p++;
131               naliases++;
132             }
133           if (*p)
134             break;
135         }
136       hostent->h_aliases[naliases] = NULL;
137
138       hostent->h_addrtype = AF_INET;
139       hostent->h_length = sizeof(struct in_addr);
140       memcpy(hostent->h_addr_list[0], &addr, sizeof(struct in_addr));
141       hostent->h_addr_list[1] = NULL;
142       *host = hostent;
143       free(line);
144       return ARES_SUCCESS;
145     }
146   if(line)
147     free(line);
148
149   if (status == ARES_SUCCESS)
150     {
151       /* Memory allocation failure; clean up. */
152       if (hostent)
153         {
154           if(hostent->h_name)
155             free((char *) hostent->h_name);
156           if (hostent->h_aliases)
157             {
158               for (alias = hostent->h_aliases; *alias; alias++)
159                 free(*alias);
160             }
161           if(hostent->h_aliases)
162             free(hostent->h_aliases);
163           if (hostent->h_addr_list && hostent->h_addr_list[0])
164             free(hostent->h_addr_list[0]);
165           if(hostent->h_addr_list)
166             free(hostent->h_addr_list);
167           free(hostent);
168         }
169       return ARES_ENOMEM;
170     }
171
172   return status;
173 }