Commit 32749085 authored by Yang Tse's avatar Yang Tse
Browse files

avoid a couple of potential zero size memory allocations

parent c7309344
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -2,6 +2,8 @@

* November 6

- Yang Tse removed a couple of potential zero size memory allocations.

- Andreas Rieke fixed the line endings in the areslib.dsp file that I (Daniel)
  broke in the 1.3.2 release. We should switch to a system where that file is
  auto-generated. We could rip some code for that from curl...
+5 −2
Original line number Diff line number Diff line
@@ -38,8 +38,11 @@ void ares_cancel(ares_channel channel)
  }
  channel->queries = NULL;
  if (!(channel->flags & ARES_FLAG_STAYOPEN))
  {
    if (channel->servers)
    {
      for (i = 0; i < channel->nservers; i++)
        ares__close_sockets(channel, &channel->servers[i]);
    }
  }
}
+28 −14
Original line number Diff line number Diff line
@@ -25,23 +25,37 @@ void ares_destroy(ares_channel channel)
  int i;
  struct query *query;

  if (!channel)
    return;

  if (channel->servers) {
    for (i = 0; i < channel->nservers; i++)
      ares__close_sockets(channel, &channel->servers[i]);
    free(channel->servers);
  }

  if (channel->domains) {
    for (i = 0; i < channel->ndomains; i++)
      free(channel->domains[i]);
    free(channel->domains);
  }

  if(channel->sortlist)
    free(channel->sortlist);

  if (channel->lookups)
    free(channel->lookups);
  while (channel->queries)
    {

  while (channel->queries) {
    query = channel->queries;
    channel->queries = query->next;
    query->callback(query->arg, ARES_EDESTRUCTION, NULL, 0);
    if (query->tcpbuf)
      free(query->tcpbuf);
    if (query->skip_server)
      free(query->skip_server);
    free(query);
  }

  free(channel);
}
+35 −19
Original line number Diff line number Diff line
@@ -125,7 +125,9 @@ int ares_init_options(ares_channel *channelptr, struct ares_options *options,
  channel->queries = NULL;
  channel->domains = NULL;
  channel->sortlist = NULL;
  channel->servers = NULL;
  channel->sock_state_cb = NULL;
  channel->sock_state_cb_data = NULL;

  /* Initialize configuration by each of the four sources, from highest
   * precedence to lowest.
@@ -140,7 +142,7 @@ int ares_init_options(ares_channel *channelptr, struct ares_options *options,
  if (status != ARES_SUCCESS)
    {
      /* Something failed; clean up memory we may have allocated. */
      if (channel->nservers != -1)
      if (channel->servers)
        free(channel->servers);
      if (channel->domains)
        {
@@ -213,13 +215,17 @@ static int init_by_options(ares_channel channel, struct ares_options *options,

  /* Copy the servers, if given. */
  if ((optmask & ARES_OPT_SERVERS) && channel->nservers == -1)
    {
      /* Avoid zero size allocations at any cost */
      if (options->nservers > 0)
        {
          channel->servers =
            malloc(options->nservers * sizeof(struct server_state));
      if (!channel->servers && options->nservers != 0)
          if (!channel->servers)
            return ARES_ENOMEM;
          for (i = 0; i < options->nservers; i++)
            channel->servers[i].addr = options->servers[i];
        }
      channel->nservers = options->nservers;
    }

@@ -227,9 +233,12 @@ static int init_by_options(ares_channel channel, struct ares_options *options,
   * we can clean up in case of error.
   */
  if ((optmask & ARES_OPT_DOMAINS) && channel->ndomains == -1)
    {
      /* Avoid zero size allocations at any cost */
      if (options->ndomains > 0)
      {
        channel->domains = malloc(options->ndomains * sizeof(char *));
      if (!channel->domains && options->ndomains != 0)
        if (!channel->domains)
          return ARES_ENOMEM;
        for (i = 0; i < options->ndomains; i++)
          {
@@ -238,6 +247,7 @@ static int init_by_options(ares_channel channel, struct ares_options *options,
            if (!channel->domains[i])
              return ARES_ENOMEM;
          }
      }
      channel->ndomains = options->ndomains;
    }

@@ -711,7 +721,6 @@ static int init_by_defaults(ares_channel channel)
      if (gethostname(hostname, sizeof(hostname)) == -1
          || !strchr(hostname, '.'))
        {
          channel->domains = malloc(0);
          channel->ndomains = 0;
        }
      else
@@ -940,6 +949,7 @@ static int set_search(ares_channel channel, const char *str)
    for(n=0; n < channel->ndomains; n++)
      free(channel->domains[n]);
    free(channel->domains);
    channel->domains = NULL;
    channel->ndomains = -1;
  }

@@ -955,8 +965,14 @@ static int set_search(ares_channel channel, const char *str)
      n++;
    }

  if (!n)
    {
      channel->ndomains = 0;
      return ARES_SUCCESS;
    }

  channel->domains = malloc(n * sizeof(char *));
  if (!channel->domains && n)
  if (!channel->domains)
    return ARES_ENOMEM;

  /* Now copy the domains. */