Fix the code that removes trailing dots

And make it easier to understand as well.
This commit is contained in:
Marco d'Itri 2014-10-24 09:21:20 +02:00
parent a34f18b7a0
commit e054c58eb2

13
whois.c
View File

@ -1110,7 +1110,7 @@ const char *is_new_gtld(const char *s)
/* /*
* Attempt to normalize a query by removing trailing dots and whitespace, * Attempt to normalize a query by removing trailing dots and whitespace,
* then convert the domain to punycode. * then convert the domain to punycode.
* The function assumes that the domain is the last token of they query. * The function assumes that the domain is the last token of the query.
* Returns a malloc'ed string which needs to be freed by the caller. * Returns a malloc'ed string which needs to be freed by the caller.
*/ */
char *normalize_domain(const char *dom) char *normalize_domain(const char *dom)
@ -1121,10 +1121,15 @@ char *normalize_domain(const char *dom)
#endif #endif
ret = strdup(dom); ret = strdup(dom);
/* eat trailing dots and blanks */ /* start from the last character */
p = ret + strlen(ret); p = ret + strlen(ret) - 1;
for (; *p == '.' || *p == ' ' || *p == '\t' || p == ret; p--) /* and then eat trailing dots and blanks */
while (p > ret) {
if (!(*p == '.' || *p == ' ' || *p == '\t'))
break;
*p = '\0'; *p = '\0';
p--;
}
#ifdef HAVE_LIBIDN #ifdef HAVE_LIBIDN
/* find the start of the last word if there are spaces in the query */ /* find the start of the last word if there are spaces in the query */