From d198cdaefde851db80ff9e179bbecb5185c0ff82 Mon Sep 17 00:00:00 2001 From: Michael Adam Date: Thu, 12 Nov 2015 11:35:07 +0100 Subject: [PATCH] nwrap: rewrite the loop for duplication ai entries if socktype not given This loop reads much more naturally now. It inserts the duplicated entry right after the entrie that is being duplicated. It does not need a ai_tail any more. Signed-off-by: Michael Adam Reviewed-by: Andreas Schneider --- lib/nss_wrapper/nss_wrapper.c | 69 ++++++++++++++++++++++--------------------- 1 file changed, 35 insertions(+), 34 deletions(-) diff --git a/lib/nss_wrapper/nss_wrapper.c b/lib/nss_wrapper/nss_wrapper.c index dabced100a3..539eb2ab47e 100644 --- a/lib/nss_wrapper/nss_wrapper.c +++ b/lib/nss_wrapper/nss_wrapper.c @@ -5261,48 +5261,49 @@ valid_port: * both UDP and TCP. */ if (hints->ai_socktype == 0) { - /* Add second ai */ - struct addrinfo *ai_head = ai; - struct addrinfo *ai_tmp; - struct addrinfo *ai_new_tail = ai_tail; - - /* Add at least one more struct */ - do { - /* CHECKS! */ - ai_tmp = malloc(sizeof(struct addrinfo)); - memcpy(ai_tmp, ai_head, sizeof(struct addrinfo)); - ai_tmp->ai_next = NULL; + struct addrinfo *ai_cur; - /* We need a deep copy or freeaddrinfo() will blow up */ - if (ai_head->ai_canonname != NULL) { - ai_tmp->ai_canonname = - strdup(ai_head->ai_canonname); + /* freeaddrinfo() frees ai_canonname and ai so allocate them */ + for (ai_cur = ai; ai_cur != NULL; ai_cur = ai_cur->ai_next) { + struct addrinfo *ai_new; + + /* duplicate the current entry */ + + ai_new = malloc(sizeof(struct addrinfo)); + if (ai_new == NULL) { + freeaddrinfo(ai); + return EAI_MEMORY; } - /* ai_head should point inside hints. */ - ai_tmp->ai_addr = ai_head->ai_addr; - if (ai_head->ai_flags == 0) { - ai_tmp->ai_flags = hints->ai_flags; + memcpy(ai_new, ai_cur, sizeof(struct addrinfo)); + ai_new->ai_next = NULL; + + /* We need a deep copy or freeaddrinfo() will blow up */ + if (ai_cur->ai_canonname != NULL) { + ai_new->ai_canonname = + strdup(ai_cur->ai_canonname); } - if (ai_head->ai_socktype == SOCK_DGRAM) { - ai_tmp->ai_socktype = SOCK_STREAM; - } else if (ai_head->ai_socktype == SOCK_STREAM) { - ai_tmp->ai_socktype = SOCK_DGRAM; + + if (ai_cur->ai_socktype == SOCK_DGRAM) { + ai_new->ai_socktype = SOCK_STREAM; + } else if (ai_cur->ai_socktype == SOCK_STREAM) { + ai_new->ai_socktype = SOCK_DGRAM; } - if (ai_head->ai_socktype == SOCK_DGRAM) { - ai_tmp->ai_protocol = IPPROTO_UDP; - } else if (ai_head->ai_socktype == SOCK_STREAM) { - ai_tmp->ai_protocol = IPPROTO_TCP; + if (ai_cur->ai_protocol == IPPROTO_TCP) { + ai_new->ai_protocol = IPPROTO_UDP; + } else if (ai_cur->ai_protocol == IPPROTO_UDP) { + ai_new->ai_protocol = IPPROTO_TCP; } - ai_new_tail->ai_next = ai_tmp; - ai_new_tail = ai_tmp; + /* now insert the new entry */ - if (ai_head == ai_tail) { - break; - } - ai_head = ai_head->ai_next; - } while (1); + ai_new->ai_next = ai_cur->ai_next; + ai_cur->ai_next = ai_new; + + /* and move on (don't duplicate the new entry) */ + + ai_cur = ai_new; + } } *res = ai; -- 2.11.4.GIT