From d35434d6bd7582cf2904f4fd23f2b9742da1ff9d Mon Sep 17 00:00:00 2001 From: Brad House Date: Sun, 15 Oct 2023 15:18:43 -0400 Subject: [PATCH] SonarCloud: Fix reported bugs SonarCloud reported a few bugs, this commit should fix those reports. Fix By: Brad House (@bradh352) --- src/lib/ares__addrinfo2hostent.c | 25 ++++++++++++++++--------- src/lib/ares_gethostbyaddr.c | 2 +- src/lib/ares_init.c | 2 ++ src/lib/ares_process.c | 1 + 4 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/lib/ares__addrinfo2hostent.c b/src/lib/ares__addrinfo2hostent.c index 4d8cdce4..081fbea6 100644 --- a/src/lib/ares__addrinfo2hostent.c +++ b/src/lib/ares__addrinfo2hostent.c @@ -66,6 +66,15 @@ ares_status_t ares__addrinfo2hostent(const struct ares_addrinfo *ai, int family, if (ai == NULL || host == NULL) return ARES_EBADQUERY; + /* Use the first node of the response as the family, since hostent can only + * represent one family. We assume getaddrinfo() returned a sorted list if + * the user requested AF_UNSPEC. */ + if (family == AF_UNSPEC && ai->nodes) + family = ai->nodes->ai_family; + + if (family != AF_INET && family != AF_INET6) + return ARES_EBADQUERY; + *host = ares_malloc(sizeof(**host)); if (!(*host)) { @@ -73,12 +82,6 @@ ares_status_t ares__addrinfo2hostent(const struct ares_addrinfo *ai, int family, } memset(*host, 0, sizeof(**host)); - /* Use the first node of the response as the family, since hostent can only - * represent one family. We assume getaddrinfo() returned a sorted list if - * the user requested AF_UNSPEC. */ - if (family == AF_UNSPEC && ai->nodes) - family = ai->nodes->ai_family; - next = ai->nodes; while (next) { @@ -148,8 +151,12 @@ ares_status_t ares__addrinfo2hostent(const struct ares_addrinfo *ai, int family, } (*host)->h_addrtype = family; - (*host)->h_length = (family == AF_INET)? - sizeof(struct in_addr):sizeof(struct ares_in6_addr); + + if (family == AF_INET) + (*host)->h_length = sizeof(struct in_addr); + + if (family == AF_INET6) + (*host)->h_length = sizeof(struct ares_in6_addr); if (naddrs) { @@ -172,7 +179,7 @@ ares_status_t ares__addrinfo2hostent(const struct ares_addrinfo *ai, int family, &(CARES_INADDR_CAST(struct sockaddr_in6 *, next->ai_addr)->sin6_addr), (size_t)(*host)->h_length); } - else + if (family == AF_INET) { memcpy((*host)->h_addr_list[i], &(CARES_INADDR_CAST(struct sockaddr_in *, next->ai_addr)->sin_addr), diff --git a/src/lib/ares_gethostbyaddr.c b/src/lib/ares_gethostbyaddr.c index b0bb197f..6da8a873 100644 --- a/src/lib/ares_gethostbyaddr.c +++ b/src/lib/ares_gethostbyaddr.c @@ -253,7 +253,7 @@ static ares_status_t file_lookup(struct ares_addr *addr, struct hostent **host) } else if (addr->family == AF_INET6) { - if (memcmp((*host)->h_addr, &addr->addrV6, + if (memcmp((*host)->h_addr, addr->addrV6._S6_un._S6_u8, sizeof(addr->addrV6)) == 0) break; } diff --git a/src/lib/ares_init.c b/src/lib/ares_init.c index 6c7d583e..6464c241 100644 --- a/src/lib/ares_init.c +++ b/src/lib/ares_init.c @@ -2120,6 +2120,8 @@ static ares_status_t config_sortlist(struct apattern **sortlist, size_t *nsort, { if (ipbufpfx[0]) { + if (q-str >= 16) + return ARES_EBADSTR; memcpy(ipbuf, str, (size_t)(q-str)); ipbuf[q-str] = '\0'; if (ip_addr(ipbuf, q-str, &pat.mask.addr4) != 0) diff --git a/src/lib/ares_process.c b/src/lib/ares_process.c index 0c80e162..5d39d5ce 100644 --- a/src/lib/ares_process.c +++ b/src/lib/ares_process.c @@ -418,6 +418,7 @@ static void read_udp_packets_fd(ares_channel channel, struct sockaddr_in sa4; struct sockaddr_in6 sa6; } from; + memset(&from, 0, sizeof(from)); #endif /* To reduce event loop overhead, read and process as many