avoid a couple of potential zero size memory allocations
[platform/upstream/c-ares.git] / ares_destroy.c
1 /* $Id$ */
2
3 /* Copyright 1998 by the Massachusetts Institute of Technology.
4  *
5  * Permission to use, copy, modify, and distribute this
6  * software and its documentation for any purpose and without
7  * fee is hereby granted, provided that the above copyright
8  * notice appear in all copies and that both that copyright
9  * notice and this permission notice appear in supporting
10  * documentation, and that the name of M.I.T. not be used in
11  * advertising or publicity pertaining to distribution of the
12  * software without specific, written prior permission.
13  * M.I.T. makes no representations about the suitability of
14  * this software for any purpose.  It is provided "as is"
15  * without express or implied warranty.
16  */
17
18 #include "setup.h"
19 #include <stdlib.h>
20 #include "ares.h"
21 #include "ares_private.h"
22
23 void ares_destroy(ares_channel channel)
24 {
25   int i;
26   struct query *query;
27
28   if (!channel)
29     return;
30
31   if (channel->servers) {
32     for (i = 0; i < channel->nservers; i++)
33       ares__close_sockets(channel, &channel->servers[i]);
34     free(channel->servers);
35   }
36
37   if (channel->domains) {
38     for (i = 0; i < channel->ndomains; i++)
39       free(channel->domains[i]);
40     free(channel->domains);
41   }
42
43   if(channel->sortlist)
44     free(channel->sortlist);
45
46   if (channel->lookups)
47     free(channel->lookups);
48
49   while (channel->queries) {
50     query = channel->queries;
51     channel->queries = query->next;
52     query->callback(query->arg, ARES_EDESTRUCTION, NULL, 0);
53     if (query->tcpbuf)
54       free(query->tcpbuf);
55     if (query->skip_server)
56       free(query->skip_server);
57     free(query);
58   }
59
60   free(channel);
61 }