From 5d93b67ca5947fd355ad6e182b01050aa0abf5f9 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Sat, 16 Jun 2018 20:11:30 +0100 Subject: [PATCH 1/5] Do not use an aliased pointer in dns_ai_setent Taken from cqueues commit 8b5bbf9dbe6c8262f3f29a8e15dec7d07993655d --- src/dns.c | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/src/dns.c b/src/dns.c index 15ff335..a6bb6a6 100644 --- a/src/dns.c +++ b/src/dns.c @@ -8195,29 +8195,23 @@ void dns_ai_close(struct dns_addrinfo *ai) { static int dns_ai_setent(struct addrinfo **ent, union dns_any *any, enum dns_type type, struct dns_addrinfo *ai) { - struct sockaddr *saddr; - struct sockaddr_in sin; - struct sockaddr_in6 sin6; + union { struct sockaddr saddr; struct sockaddr_in sin; struct sockaddr_in6 sin6; } saddr = {0}; const char *cname; size_t clen; switch (type) { case DNS_T_A: - saddr = memset(&sin, '\0', sizeof sin); + saddr.sin.sin_family = AF_INET; + saddr.sin.sin_port = htons(ai->port); - sin.sin_family = AF_INET; - sin.sin_port = htons(ai->port); - - memcpy(&sin.sin_addr, any, sizeof sin.sin_addr); + memcpy(&saddr.sin.sin_addr, any, sizeof saddr.sin.sin_addr); break; case DNS_T_AAAA: - saddr = memset(&sin6, '\0', sizeof sin6); - - sin6.sin6_family = AF_INET6; - sin6.sin6_port = htons(ai->port); + saddr.sin6.sin6_family = AF_INET6; + saddr.sin6.sin6_port = htons(ai->port); - memcpy(&sin6.sin6_addr, any, sizeof sin6.sin6_addr); + memcpy(&saddr.sin6.sin6_addr, any, sizeof saddr.sin6.sin6_addr); break; default: @@ -8232,20 +8226,20 @@ static int dns_ai_setent(struct addrinfo **ent, union dns_any *any, enum dns_typ clen = 0; } - if (!(*ent = malloc(sizeof **ent + dns_sa_len(saddr) + ((ai->hints.ai_flags & AI_CANONNAME)? clen + 1 : 0)))) + if (!(*ent = malloc(sizeof **ent + dns_sa_len(&saddr) + ((ai->hints.ai_flags & AI_CANONNAME)? clen + 1 : 0)))) return dns_syerr(); memset(*ent, '\0', sizeof **ent); - (*ent)->ai_family = saddr->sa_family; + (*ent)->ai_family = saddr.saddr.sa_family; (*ent)->ai_socktype = ai->hints.ai_socktype; (*ent)->ai_protocol = ai->hints.ai_protocol; - (*ent)->ai_addr = memcpy((unsigned char *)*ent + sizeof **ent, saddr, dns_sa_len(saddr)); - (*ent)->ai_addrlen = dns_sa_len(saddr); + (*ent)->ai_addr = memcpy((unsigned char *)*ent + sizeof **ent, &saddr, dns_sa_len(&saddr)); + (*ent)->ai_addrlen = dns_sa_len(&saddr); if (ai->hints.ai_flags & AI_CANONNAME) - (*ent)->ai_canonname = memcpy((unsigned char *)*ent + sizeof **ent + dns_sa_len(saddr), cname, clen + 1); + (*ent)->ai_canonname = memcpy((unsigned char *)*ent + sizeof **ent + dns_sa_len(&saddr), cname, clen + 1); ai->found++; From 0ac72c9ec218cc1e4bc3113d5866a76f5c193b0c Mon Sep 17 00:00:00 2001 From: daurnimator Date: Sat, 16 Jun 2018 20:13:14 +0100 Subject: [PATCH 2/5] If gethostname() is not a label then don't add search path --- src/dns.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/dns.c b/src/dns.c index a6bb6a6..b0de0cc 100644 --- a/src/dns.c +++ b/src/dns.c @@ -4419,6 +4419,7 @@ struct dns_resolv_conf *dns_resconf_open(int *error) { }; struct dns_resolv_conf *resconf; struct sockaddr_in *sin; + size_t len; if (!(resconf = malloc(sizeof *resconf))) goto syerr; @@ -4436,13 +4437,11 @@ struct dns_resolv_conf *dns_resconf_open(int *error) { if (0 != gethostname(resconf->search[0], sizeof resconf->search[0])) goto syerr; - dns_d_anchor(resconf->search[0], sizeof resconf->search[0], resconf->search[0], strlen(resconf->search[0])); - dns_d_cleave(resconf->search[0], sizeof resconf->search[0], resconf->search[0], strlen(resconf->search[0])); - - /* - * XXX: If gethostname() returned a string without any label - * separator, then search[0][0] should be NUL. - */ + len = strlen(resconf->search[0]); + len = dns_d_anchor(resconf->search[0], sizeof resconf->search[0], resconf->search[0], len); + len = dns_d_cleave(resconf->search[0], sizeof resconf->search[0], resconf->search[0], len); + if (1 == len) /* gethostname() returned a string without any label */ + resconf->search[0][0] = '\0'; dns_resconf_acquire(resconf); From f3a377868961551b5611bd8b7bb19acd600501da Mon Sep 17 00:00:00 2001 From: daurnimator Date: Sat, 16 Jun 2018 20:23:21 +0100 Subject: [PATCH 3/5] Fix -Wexpansion-to-defined warnings Closes #25 --- src/dns.c | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/src/dns.c b/src/dns.c index b0de0cc..0691e74 100644 --- a/src/dns.c +++ b/src/dns.c @@ -203,10 +203,11 @@ #endif #ifndef HAVE_STATIC_ASSERT -#if DNS_GNUC_PREREQ(0,0,0) && !DNS_GNUC_PREREQ(4,6,0) -#define HAVE_STATIC_ASSERT 0 /* glibc doesn't check GCC version */ +#if (defined static_assert) && \ + (!DNS_GNUC_PREREQ(0,0,0) || DNS_GNUC_PREREQ(4,6,0)) /* glibc doesn't check GCC version */ +#define HAVE_STATIC_ASSERT 1 #else -#define HAVE_STATIC_ASSERT (defined static_assert) +#define HAVE_STATIC_ASSERT 0 #endif #endif @@ -372,7 +373,11 @@ const char *dns_strerror(int error) { * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ #ifndef HAVE___ATOMIC_FETCH_ADD -#define HAVE___ATOMIC_FETCH_ADD (defined __ATOMIC_RELAXED) +#ifdef __ATOMIC_RELAXED +#define HAVE___ATOMIC_FETCH_ADD 1 +#else +#define HAVE___ATOMIC_FETCH_ADD 0 +#endif #endif #ifndef HAVE___ATOMIC_FETCH_SUB @@ -779,7 +784,11 @@ DNS_NOTUSED static size_t dns_strnlcpy(char *dst, size_t lim, const char *src, s } /* dns_strnlcpy() */ -#define DNS_HAVE_SOCKADDR_UN (defined AF_UNIX && !defined _WIN32) +#if (defined AF_UNIX && !defined _WIN32) +#define DNS_HAVE_SOCKADDR_UN 1 +#else +#define DNS_HAVE_SOCKADDR_UN 0 +#endif static size_t dns_af_len(int af) { static const size_t table[AF_MAX] = { @@ -6114,11 +6123,19 @@ static void dns_socketclose(int *fd, const struct dns_options *opts) { #endif #ifndef HAVE_SOCK_CLOEXEC -#define HAVE_SOCK_CLOEXEC (defined SOCK_CLOEXEC) +#ifdef SOCK_CLOEXEC +#define HAVE_SOCK_CLOEXEC 1 +#else +#define HAVE_SOCK_CLOEXEC 0 +#endif #endif #ifndef HAVE_SOCK_NONBLOCK -#define HAVE_SOCK_NONBLOCK (defined SOCK_NONBLOCK) +#ifdef SOCK_NONBLOCK +#define HAVE_SOCK_NONBLOCK 1 +#else +#define HAVE_SOCK_NONBLOCK 0 +#endif #endif #define DNS_SO_MAXTRY 7 From 1192853a8ce4727f1523d4ec81908f3ccef0d64b Mon Sep 17 00:00:00 2001 From: daurnimator Date: Sat, 16 Jun 2018 20:26:12 +0100 Subject: [PATCH 4/5] Add FALL THROUGH annotations to avoid warnings --- src/dns.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/dns.c b/src/dns.c index 0691e74..54cd3b3 100644 --- a/src/dns.c +++ b/src/dns.c @@ -6611,11 +6611,13 @@ int dns_so_check(struct dns_socket *so) { switch (so->state) { case DNS_SO_UDP_INIT: so->state++; + /* FALL THROUGH */ case DNS_SO_UDP_CONN: if (0 != connect(so->udp, (struct sockaddr *)&so->remote, dns_sa_len(&so->remote))) goto soerr; so->state++; + /* FALL THROUGH */ case DNS_SO_UDP_SEND: if (0 > (n = send(so->udp, (void *)so->query->data, so->query->end, 0))) goto soerr; @@ -6624,6 +6626,7 @@ int dns_so_check(struct dns_socket *so) { so->stat.udp.sent.count++; so->state++; + /* FALL THROUGH */ case DNS_SO_UDP_RECV: if (0 > (n = recv(so->udp, (void *)so->answer->data, so->answer->size, 0))) goto soerr; @@ -6638,11 +6641,13 @@ int dns_so_check(struct dns_socket *so) { goto trash; so->state++; + /* FALL THROUGH */ case DNS_SO_UDP_DONE: if (!dns_header(so->answer)->tc || so->type == SOCK_DGRAM) return 0; so->state++; + /* FALL THROUGH */ case DNS_SO_TCP_INIT: if (dns_so_tcp_keep(so)) { so->state = DNS_SO_TCP_SEND; @@ -6657,6 +6662,7 @@ int dns_so_check(struct dns_socket *so) { goto error; so->state++; + /* FALL THROUGH */ case DNS_SO_TCP_CONN: if (0 != connect(so->tcp, (struct sockaddr *)&so->remote, dns_sa_len(&so->remote))) { if (dns_soerr() != DNS_EISCONN) @@ -6664,16 +6670,19 @@ int dns_so_check(struct dns_socket *so) { } so->state++; + /* FALL THROUGH */ case DNS_SO_TCP_SEND: if ((error = dns_so_tcp_send(so))) goto error; so->state++; + /* FALL THROUGH */ case DNS_SO_TCP_RECV: if ((error = dns_so_tcp_recv(so))) goto error; so->state++; + /* FALL THROUGH */ case DNS_SO_TCP_DONE: /* close unless DNS_RESCONF_TCP_ONLY (see dns_res_tcp2type) */ if (so->type != SOCK_STREAM) { @@ -7285,6 +7294,7 @@ static int dns_res_exec(struct dns_resolver *R) { switch (F->state) { case DNS_R_INIT: F->state++; + /* FALL THROUGH */ case DNS_R_GLUE: if (R->sp == 0) dgoto(R->sp, DNS_R_SWITCH); @@ -7311,6 +7321,7 @@ static int dns_res_exec(struct dns_resolver *R) { } F->state++; + /* FALL THROUGH */ case DNS_R_SWITCH: while (F->which < (int)sizeof R->resconf->lookup && R->resconf->lookup[F->which]) { switch (R->resconf->lookup[F->which++]) { @@ -7399,16 +7410,19 @@ static int dns_res_exec(struct dns_resolver *R) { goto error; F->state++; + /* FALL THROUGH */ case DNS_R_SUBMIT: if ((error = R->cache->submit(F->query, R->cache))) goto error; F->state++; + /* FALL THROUGH */ case DNS_R_CHECK: if ((error = R->cache->check(R->cache))) goto error; F->state++; + /* FALL THROUGH */ case DNS_R_FETCH: error = 0; @@ -7434,6 +7448,7 @@ static int dns_res_exec(struct dns_resolver *R) { R->search = 0; F->state++; + /* FALL THROUGH */ case DNS_R_SEARCH: /* * XXX: We probably should only apply the domain search @@ -7446,11 +7461,13 @@ static int dns_res_exec(struct dns_resolver *R) { goto error; F->state++; + /* FALL THROUGH */ case DNS_R_HINTS: if (!dns_p_setptr(&F->hints, dns_hints_query(R->hints, F->query, &error))) goto error; F->state++; + /* FALL THROUGH */ case DNS_R_ITERATE: dns_rr_i_init(&F->hints_i, F->hints); @@ -7460,6 +7477,7 @@ static int dns_res_exec(struct dns_resolver *R) { F->hints_i.args[0] = F->hints->end; F->state++; + /* FALL THROUGH */ case DNS_R_FOREACH_NS: dns_rr_i_save(&F->hints_i); @@ -7549,6 +7567,7 @@ static int dns_res_exec(struct dns_resolver *R) { F->state++; } + /* FALL THROUGH */ case DNS_R_QUERY_A: if (dns_so_elapsed(&R->so) >= dns_resconf_timeout(R->resconf)) dgoto(R->sp, DNS_R_FOREACH_A); @@ -7652,6 +7671,7 @@ static int dns_res_exec(struct dns_resolver *R) { dns_rr_i_init(&R->smart, F->answer); F->state++; + /* FALL THROUGH */ case DNS_R_SMART0_A: if (&F[1] >= endof(R->stack)) dgoto(R->sp, DNS_R_DONE); @@ -8297,11 +8317,13 @@ int dns_ai_nextent(struct addrinfo **ent, struct dns_addrinfo *ai) { switch (ai->state) { case DNS_AI_S_INIT: ai->state++; + /* FALL THROUGH */ case DNS_AI_S_NEXTAF: if (!dns_ai_nextaf(ai)) dns_ai_goto(DNS_AI_S_DONE); ai->state++; + /* FALL THROUGH */ case DNS_AI_S_NUMERIC: if (1 == dns_inet_pton(AF_INET, ai->qname, &any.a)) { if (ai->af.atype == AF_INET) { @@ -8325,6 +8347,7 @@ int dns_ai_nextent(struct addrinfo **ent, struct dns_addrinfo *ai) { dns_ai_goto(DNS_AI_S_NEXTAF); ai->state++; + /* FALL THROUGH */ case DNS_AI_S_SUBMIT: assert(ai->res); @@ -8332,11 +8355,13 @@ int dns_ai_nextent(struct addrinfo **ent, struct dns_addrinfo *ai) { return error; ai->state++; + /* FALL THROUGH */ case DNS_AI_S_CHECK: if ((error = dns_res_check(ai->res))) return error; ai->state++; + /* FALL THROUGH */ case DNS_AI_S_FETCH: if (!(ans = dns_res_fetch_and_study(ai->res, &error))) return error; @@ -8360,6 +8385,7 @@ int dns_ai_nextent(struct addrinfo **ent, struct dns_addrinfo *ai) { ai->i.sort = &dns_rr_i_order; ai->state++; + /* FALL THROUGH */ case DNS_AI_S_FOREACH_I: if (!dns_rr_grep(&rr, 1, &ai->i, ai->answer, &error)) dns_ai_goto(DNS_AI_S_NEXTAF); @@ -8394,10 +8420,12 @@ int dns_ai_nextent(struct addrinfo **ent, struct dns_addrinfo *ai) { } /* switch() */ ai->state++; + /* FALL THROUGH */ case DNS_AI_S_INIT_G: ai->g_depth = 0; ai->state++; + /* FALL THROUGH */ case DNS_AI_S_ITERATE_G: dns_strlcpy(ai->g_cname, ai->cname, sizeof ai->g_cname); dns_rr_i_init(&ai->g, ai->glue); @@ -8406,6 +8434,7 @@ int dns_ai_nextent(struct addrinfo **ent, struct dns_addrinfo *ai) { ai->g.type = ai->af.qtype; ai->state++; + /* FALL THROUGH */ case DNS_AI_S_FOREACH_G: if (!dns_rr_grep(&rr, 1, &ai->g, ai->glue, &error)) { if (dns_rr_i_count(&ai->g) > 0) @@ -8430,11 +8459,13 @@ int dns_ai_nextent(struct addrinfo **ent, struct dns_addrinfo *ai) { return error; ai->state++; + /* FALL THROUGH */ case DNS_AI_S_CHECK_G: if ((error = dns_res_check(ai->res))) return error; ai->state++; + /* FALL THROUGH */ case DNS_AI_S_FETCH_G: if (!(ans = dns_res_fetch_and_study(ai->res, &error))) return error; From 101e032c28960153a319c93af62da648c2261022 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Sat, 16 Jun 2018 20:28:32 +0100 Subject: [PATCH 5/5] define _DEFAULT_SOURCE to prevent glibc warning See https://sourceware.org/bugzilla/show_bug.cgi?id=16632 --- src/dns.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/dns.c b/src/dns.c index 54cd3b3..23a350b 100644 --- a/src/dns.c +++ b/src/dns.c @@ -28,6 +28,9 @@ #define _XOPEN_SOURCE 600 #endif +#undef _DEFAULT_SOURCE +#define _DEFAULT_SOURCE + #undef _BSD_SOURCE #define _BSD_SOURCE