diff options
Diffstat (limited to 'dns.c')
-rw-r--r-- | dns.c | 27 |
1 files changed, 21 insertions, 6 deletions
@@ -110,6 +110,10 @@ struct dns * dns_init (void) { return NULL; dns->fd = -1; dns->domain = strdup(" call dns_set_domain to set the domain"); + if (!dns->domain) { + free(dns); + return NULL; + } dns->domain[0] = strlen(dns->domain)-1; dns->log_handler = dns_default_log_handler; dns->sockaddr.sin_family = AF_INET; @@ -117,12 +121,23 @@ struct dns * dns_init (void) { dns->sockaddr.sin_addr.s_addr = INADDR_ANY; return dns; } -static void dns_set_domain (struct dns * dns, const char * domain) { // static functions are - int required = domain2name_len(domain, strlen(domain)); // visible inside the same - if (required <= 0) // translation unit - they - return; // are visible across - free(dns->domain); // included files - domain2name((dns->domain = malloc(required)), domain, strlen(domain)); +static void dns_set_domain (struct dns * dns, const char * domain) { + char buf[64]; + int required = domain2name_len(domain, strlen(domain)); + if (required <= 0) { + sprintf(buf, "domain2name_len failed with %d", required); + dns->log_handler(dns->log_userdata, DNS_ERROR, "dns", buf); + return; + } + char * new = malloc(required); + if (!new) { + sprintf(buf, "malloc of size %d failed", required); + dns->log_handler(dns->log_userdata, DNS_ERROR, "dns", buf); + return; + } + free(dns->domain); + dns->domain = new; + domain2name(dns->domain, domain, strlen(domain)); } static void dns_set_log_handler (struct dns * dns, dns_log_handler log_handler) { if (!log_handler) |