From 1dc228c872974d7eafc34b53816e973e00224351 Mon Sep 17 00:00:00 2001 From: kedixa <1204837541@qq.com> Date: Tue, 9 Apr 2019 07:37:43 +0800 Subject: [PATCH 05/10] getaddrinfo: avoid infinite loop in case of NXDOMAIN(#240) (#242) There are two possible causes for infinite loops fo NXDOMAIN, based on how many dots are in the domain name (one for < ARES_OPT_NDOTS and one for >= ARES_OPT_NDOTS), where it will repeat the same query over and over as the hquery->next_domain doesn't increment. Fix By: @kedixa --- ares_getaddrinfo.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/ares_getaddrinfo.c b/ares_getaddrinfo.c index ebaeda8..16c8b38 100644 --- a/ares_getaddrinfo.c +++ b/ares_getaddrinfo.c @@ -122,7 +122,7 @@ void ares_getaddrinfo(ares_channel channel, hquery->callback = callback; hquery->arg = arg; hquery->timeouts = 0; - hquery->next_domain = 0; + hquery->next_domain = -1; /* see next_dns_lookup for more info */ hquery->remaining = 0; /* Host file lookup */ @@ -279,11 +279,20 @@ static void next_dns_lookup(struct host_query *hquery) { char *s = NULL; int is_s_allocated = 0; int status; - - if (( as_is_first(hquery) && hquery->next_domain == 0) || - (!as_is_first(hquery) && hquery->next_domain == - hquery->channel->ndomains)) { - s = hquery->name; + /* if next_domain == -1 and as_is_first is true, try hquery->name */ + if(hquery->next_domain == -1) { + if(as_is_first(hquery)) { + s = hquery->name; + } + hquery->next_domain = 0; + } + /* if as_is_first is false, try hquery->name at last */ + if(!s && hquery->next_domain == hquery->channel->ndomains) { + if(!as_is_first(hquery)) { + s = hquery->name; + } + /* avoid infinite loop */ + hquery->next_domain++; } if (!s && hquery->next_domain < hquery->channel->ndomains) { -- 2.16.4