ares_getaddrinfo using service of "0" should be allowed

As per #517 glibc allows a service/servname of "0" to be treated the
same as if NULL was provided.  Also, add a sanity check to ensure
the port number is in range instead of a blind cast.

Fixes: #517
Fix By: Brad House (@bradh352)
pull/520/head
bradh352 2 years ago
parent 38b30bc922
commit f95ca36cde
  1. 14
      src/lib/ares_getaddrinfo.c

@ -670,26 +670,32 @@ void ares_getaddrinfo(ares_channel channel,
{ {
if (hints->ai_flags & ARES_AI_NUMERICSERV) if (hints->ai_flags & ARES_AI_NUMERICSERV)
{ {
port = (unsigned short)strtoul(service, NULL, 0); unsigned long val;
if (!port) errno = 0;
val = strtoul(service, NULL, 0);
if ((val == 0 && errno != 0) || val > 65535)
{ {
ares_free(alias_name); ares_free(alias_name);
callback(arg, ARES_ESERVICE, 0, NULL); callback(arg, ARES_ESERVICE, 0, NULL);
return; return;
} }
port = (unsigned short)val;
} }
else else
{ {
port = lookup_service(service, 0); port = lookup_service(service, 0);
if (!port) if (!port)
{ {
port = (unsigned short)strtoul(service, NULL, 0); unsigned long val;
if (!port) errno = 0;
val = strtoul(service, NULL, 0);
if ((val == 0 && errno != 0) || val > 65535)
{ {
ares_free(alias_name); ares_free(alias_name);
callback(arg, ARES_ESERVICE, 0, NULL); callback(arg, ARES_ESERVICE, 0, NULL);
return; return;
} }
port = (unsigned short)val;
} }
} }
} }

Loading…
Cancel
Save