From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS17314 8.43.84.0/22 X-Spam-Status: No, score=-3.6 required=3.0 tests=AWL,BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, PDS_RDNS_DYNAMIC_FP,RCVD_IN_DNSWL_MED,RDNS_DYNAMIC,SPF_HELO_PASS, SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id A39F51F8C6 for ; Tue, 3 Aug 2021 21:33:56 +0000 (UTC) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id CD7743853C2B for ; Tue, 3 Aug 2021 21:33:55 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CD7743853C2B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1628026435; bh=t64dqFSZhayjTngTeHwHEPUM3MS+UB0vv0e3ISTzRkI=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=paIUXFdnqzu/xEjuQW1mFHhKCrNw2RJU2Rwpq/A8bYv/YbiRR+Ak6YpGJGz6/JcLq 0u8dMCy1bXetVI48g+NNlsQ0ADAfCERg2oM7ezueMyRk/RlNMVl2mOklCVWyvwOXmt zIdpkDyjSxowKvJN3gurbpJ8NSk6HngvZ3GKsBkU= Received: from buffalo.birch.relay.mailchannels.net (buffalo.birch.relay.mailchannels.net [23.83.209.24]) by sourceware.org (Postfix) with ESMTPS id 0C8FE384F014 for ; Tue, 3 Aug 2021 21:29:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 0C8FE384F014 X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from relay.mailchannels.net (localhost [127.0.0.1]) by relay.mailchannels.net (Postfix) with ESMTP id D521248150B for ; Tue, 3 Aug 2021 21:29:40 +0000 (UTC) Received: from pdx1-sub0-mail-a4.g.dreamhost.com (100-96-13-120.trex.outbound.svc.cluster.local [100.96.13.120]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id 82296482501 for ; Tue, 3 Aug 2021 21:29:40 +0000 (UTC) X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from pdx1-sub0-mail-a4.g.dreamhost.com (pop.dreamhost.com [64.90.62.162]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384) by 100.96.13.120 (trex/6.3.3); Tue, 03 Aug 2021 21:29:40 +0000 X-MC-Relay: Neutral X-MailChannels-SenderId: dreamhost|x-authsender|siddhesh@gotplt.org X-MailChannels-Auth-Id: dreamhost X-Power-Exultant: 533138f8203a1f67_1628026180771_1465703800 X-MC-Loop-Signature: 1628026180771:2189012621 X-MC-Ingress-Time: 1628026180770 Received: from pdx1-sub0-mail-a4.g.dreamhost.com (localhost [127.0.0.1]) by pdx1-sub0-mail-a4.g.dreamhost.com (Postfix) with ESMTP id 44DFE8AAA8 for ; Tue, 3 Aug 2021 14:29:40 -0700 (PDT) Received: from rhbox.redhat.com (unknown [1.186.101.110]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: siddhesh@gotplt.org) by pdx1-sub0-mail-a4.g.dreamhost.com (Postfix) with ESMTPSA id 3D7DD8AAAA for ; Tue, 3 Aug 2021 14:29:38 -0700 (PDT) X-DH-BACKEND: pdx1-sub0-mail-a4 To: libc-alpha@sourceware.org Subject: [PATCH 5/5] gaih_inet: Make process_list label into a function Date: Wed, 4 Aug 2021 02:59:19 +0530 Message-Id: <20210803212919.3059194-6-siddhesh@sourceware.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210803212919.3059194-1-siddhesh@sourceware.org> References: <20210803212919.3059194-1-siddhesh@sourceware.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Siddhesh Poyarekar via Libc-alpha Reply-To: Siddhesh Poyarekar Errors-To: libc-alpha-bounces+e=80x24.org@sourceware.org Sender: "Libc-alpha" process_list is the final operation in gaih_inet where the result is written out. Factor it out into a separate function and call it at the end if there have been no errors. Rename the free_and_return label to done and use it to jump to the end of gaih_inet, where process_list is called if return is non-zero. This change makes the code more linear and slightly clearer to follow. This needs still more rework to make the function simpler to understand. --- sysdeps/posix/getaddrinfo.c | 311 +++++++++++++++++------------------- 1 file changed, 149 insertions(+), 162 deletions(-) diff --git a/sysdeps/posix/getaddrinfo.c b/sysdeps/posix/getaddrinfo.c index 220cd41cde..3afac87fa2 100644 --- a/sysdeps/posix/getaddrinfo.c +++ b/sysdeps/posix/getaddrinfo.c @@ -256,7 +256,7 @@ convert_hostent_to_gaih_addrtuple (const struct addri= nfo *req, { \ __resolv_context_put (res_ctx); \ result =3D -EAI_MEMORY; \ - goto free_and_return; \ + goto done; \ } \ } \ if (status =3D=3D NSS_STATUS_NOTFOUND \ @@ -266,7 +266,7 @@ convert_hostent_to_gaih_addrtuple (const struct addri= nfo *req, { \ __resolv_context_put (res_ctx); \ result =3D -EAI_SYSTEM; \ - goto free_and_return; \ + goto done; \ } \ if (h_errno =3D=3D TRY_AGAIN) \ no_data =3D EAI_AGAIN; \ @@ -279,7 +279,7 @@ convert_hostent_to_gaih_addrtuple (const struct addri= nfo *req, { \ __resolv_context_put (res_ctx); \ result =3D -EAI_SYSTEM; \ - goto free_and_return; \ + goto done; \ } \ *pat =3D addrmem; \ \ @@ -290,7 +290,7 @@ convert_hostent_to_gaih_addrtuple (const struct addri= nfo *req, { \ __resolv_context_put (res_ctx); \ result =3D -EAI_SYSTEM; \ - goto free_and_return; \ + goto done; \ } \ canon =3D canonbuf; \ } \ @@ -323,6 +323,123 @@ getcanonname (nss_action_list nip, struct gaih_addr= tuple *at, const char *name) return __strdup (name); } =20 +static int +process_list (const struct addrinfo *req, struct gaih_addrtuple *at, + struct gaih_servtuple *st, const char *canon, bool canon_alloc, + bool got_ipv6, struct addrinfo **pai, unsigned int *naddrs) +{ + struct gaih_servtuple *st2; + struct gaih_addrtuple *at2 =3D at; + size_t socklen; + sa_family_t family; + + /* + buffer is the size of an unformatted IPv6 address in printable form= at. + */ + for (at2 =3D at; at2 !=3D NULL; at2 =3D at2->next) + { + /* Only the first entry gets the canonical name. */ + if (at2 =3D=3D at && (req->ai_flags & AI_CANONNAME) !=3D 0) + { + bool do_idn =3D req->ai_flags & AI_CANONIDN; + if (do_idn) + { + char *out; + int rc =3D __idna_from_dns_encoding (canon, &out); + if (rc =3D=3D 0) + canon =3D out; + else if (rc =3D=3D EAI_IDN_ENCODE) + /* Use the punycode name as a fallback. */ + do_idn =3D false; + else + return -rc; + } + if (!do_idn) + { + if (!canon_alloc) + { + canon =3D __strdup (canon); + if (canon =3D=3D NULL) + return -EAI_MEMORY; + } + } + } + + family =3D at2->family; + if (family =3D=3D AF_INET6) + { + socklen =3D sizeof (struct sockaddr_in6); + + /* If we looked up IPv4 mapped address discard them here if + the caller isn't interested in all address and we have + found at least one IPv6 address. */ + if (got_ipv6 + && (req->ai_flags & (AI_V4MAPPED|AI_ALL)) =3D=3D AI_V4MAPPED + && IN6_IS_ADDR_V4MAPPED (at2->addr)) + continue; + } + else + socklen =3D sizeof (struct sockaddr_in); + + for (st2 =3D st; st2 !=3D NULL; st2 =3D st2->next) + { + struct addrinfo *ai; + ai =3D *pai =3D malloc (sizeof (struct addrinfo) + socklen); + if (ai =3D=3D NULL) + { + free ((char *) canon); + return -EAI_MEMORY; + } + + ai->ai_flags =3D req->ai_flags; + ai->ai_family =3D family; + ai->ai_socktype =3D st2->socktype; + ai->ai_protocol =3D st2->protocol; + ai->ai_addrlen =3D socklen; + ai->ai_addr =3D (void *) (ai + 1); + + /* We only add the canonical name once. */ + ai->ai_canonname =3D (char *) canon; + canon =3D NULL; + +#ifdef _HAVE_SA_LEN + ai->ai_addr->sa_len =3D socklen; +#endif /* _HAVE_SA_LEN */ + ai->ai_addr->sa_family =3D family; + + /* In case of an allocation error the list must be NULL + terminated. */ + ai->ai_next =3D NULL; + + if (family =3D=3D AF_INET6) + { + struct sockaddr_in6 *sin6p =3D + (struct sockaddr_in6 *) ai->ai_addr; + + sin6p->sin6_port =3D st2->port; + sin6p->sin6_flowinfo =3D 0; + memcpy (&sin6p->sin6_addr, + at2->addr, sizeof (struct in6_addr)); + sin6p->sin6_scope_id =3D at2->scopeid; + } + else + { + struct sockaddr_in *sinp =3D + (struct sockaddr_in *) ai->ai_addr; + sinp->sin_port =3D st2->port; + memcpy (&sinp->sin_addr, + at2->addr, sizeof (struct in_addr)); + memset (sinp->sin_zero, '\0', sizeof (sinp->sin_zero)); + } + + pai =3D &(ai->ai_next); + } + + ++*naddrs; + } + return 0; +} + static int gaih_inet (const char *name, const struct gaih_service *service, const struct addrinfo *req, struct addrinfo **pai, @@ -479,7 +596,7 @@ gaih_inet (const char *name, const struct gaih_servic= e *service, else { result =3D -EAI_ADDRFAMILY; - goto free_and_return; + goto done; } =20 if (req->ai_flags & AI_CANONNAME) @@ -507,7 +624,7 @@ gaih_inet (const char *name, const struct gaih_servic= e *service, else { result =3D -EAI_ADDRFAMILY; - goto free_and_return; + goto done; } =20 if (scope_delim !=3D NULL @@ -516,7 +633,7 @@ gaih_inet (const char *name, const struct gaih_servic= e *service, &at->scopeid) !=3D 0) { result =3D -EAI_NONAME; - goto free_and_return; + goto done; } =20 if (req->ai_flags & AI_CANONNAME) @@ -555,7 +672,7 @@ gaih_inet (const char *name, const struct gaih_servic= e *service, if (!scratch_buffer_grow (tmpbuf)) { result =3D -EAI_MEMORY; - goto free_and_return; + goto done; } } =20 @@ -568,7 +685,7 @@ gaih_inet (const char *name, const struct gaih_servic= e *service, (req, AF_INET, h, &addrmem)) { result =3D -EAI_MEMORY; - goto free_and_return; + goto done; } *pat =3D addrmem; } @@ -578,7 +695,7 @@ gaih_inet (const char *name, const struct gaih_servic= e *service, result =3D -EAI_NODATA; else result =3D -EAI_NONAME; - goto free_and_return; + goto done; } } else @@ -592,10 +709,13 @@ gaih_inet (const char *name, const struct gaih_serv= ice *service, The name is known, though. */ result =3D -EAI_NODATA; =20 - goto free_and_return; + goto done; } =20 - goto process_list; + if (at->family =3D=3D AF_UNSPEC) + result =3D -EAI_NONAME; + + goto done; } =20 #ifdef USE_NSCD @@ -619,7 +739,7 @@ gaih_inet (const char *name, const struct gaih_servic= e *service, if (addrmem =3D=3D NULL) { result =3D -EAI_MEMORY; - goto free_and_return; + goto done; } =20 struct gaih_addrtuple *addrfree =3D addrmem; @@ -654,7 +774,7 @@ gaih_inet (const char *name, const struct gaih_servic= e *service, if (canonbuf =3D=3D NULL) { result =3D -EAI_MEMORY; - goto free_and_return; + goto done; } canon =3D (*pat)->name =3D canonbuf; } @@ -687,16 +807,13 @@ gaih_inet (const char *name, const struct gaih_serv= ice *service, free (air); =20 if (at->family =3D=3D AF_UNSPEC) - { - result =3D -EAI_NONAME; - goto free_and_return; - } + result =3D -EAI_NONAME; =20 - goto process_list; + goto done; } else if (err =3D=3D 0) /* The database contains a negative entry. */ - goto free_and_return; + goto done; else if (__nss_not_use_nscd_hosts =3D=3D 0) { if (h_errno =3D=3D NETDB_INTERNAL && errno =3D=3D ENOMEM) @@ -706,7 +823,7 @@ gaih_inet (const char *name, const struct gaih_servic= e *service, else result =3D -EAI_SYSTEM; =20 - goto free_and_return; + goto done; } } #endif @@ -755,7 +872,7 @@ gaih_inet (const char *name, const struct gaih_servic= e *service, { __resolv_context_put (res_ctx); result =3D -EAI_MEMORY; - goto free_and_return; + goto done; } } =20 @@ -854,7 +971,7 @@ gaih_inet (const char *name, const struct gaih_servic= e *service, { __resolv_context_put (res_ctx); result =3D -EAI_MEMORY; - goto free_and_return; + goto done; } canon =3D canonbuf; } @@ -903,7 +1020,7 @@ gaih_inet (const char *name, const struct gaih_servi= ce *service, && h_errno =3D=3D NETDB_INTERNAL) { result =3D -EAI_SYSTEM; - goto free_and_return; + goto done; } =20 if (no_data !=3D 0 && no_inet6_data !=3D 0) @@ -916,16 +1033,12 @@ gaih_inet (const char *name, const struct gaih_ser= vice *service, is known, though. */ result =3D -EAI_NODATA; =20 - goto free_and_return; + goto done; } } =20 - process_list: if (at->family =3D=3D AF_UNSPEC) - { - result =3D -EAI_NONAME; - goto free_and_return; - } + result =3D -EAI_NONAME; } else { @@ -955,142 +1068,16 @@ gaih_inet (const char *name, const struct gaih_se= rvice *service, } } =20 - { - struct gaih_servtuple *st2; - struct gaih_addrtuple *at2 =3D at; - size_t socklen; - sa_family_t family; - - /* - buffer is the size of an unformatted IPv6 address in printable for= mat. - */ - while (at2 !=3D NULL) - { - /* Only the first entry gets the canonical name. */ - if (at2 =3D=3D at && (req->ai_flags & AI_CANONNAME) !=3D 0) - { - if (canon =3D=3D NULL) - /* If the canonical name cannot be determined, use - the passed in string. */ - canon =3D orig_name; - - bool do_idn =3D req->ai_flags & AI_CANONIDN; - if (do_idn) - { - char *out; - int rc =3D __idna_from_dns_encoding (canon, &out); - if (rc =3D=3D 0) - canon =3D out; - else if (rc =3D=3D EAI_IDN_ENCODE) - /* Use the punycode name as a fallback. */ - do_idn =3D false; - else - { - result =3D -rc; - goto free_and_return; - } - } - if (!do_idn) - { - if (canonbuf !=3D NULL) - /* We already allocated the string using malloc, but - the buffer is now owned by canon. */ - canonbuf =3D NULL; - else - { - canon =3D __strdup (canon); - if (canon =3D=3D NULL) - { - result =3D -EAI_MEMORY; - goto free_and_return; - } - } - } - } - - family =3D at2->family; - if (family =3D=3D AF_INET6) - { - socklen =3D sizeof (struct sockaddr_in6); - - /* If we looked up IPv4 mapped address discard them here if - the caller isn't interested in all address and we have - found at least one IPv6 address. */ - if (got_ipv6 - && (req->ai_flags & (AI_V4MAPPED|AI_ALL)) =3D=3D AI_V4MAPPED - && IN6_IS_ADDR_V4MAPPED (at2->addr)) - goto ignore; - } - else - socklen =3D sizeof (struct sockaddr_in); - - for (st2 =3D st; st2 !=3D NULL; st2 =3D st2->next) - { - struct addrinfo *ai; - ai =3D *pai =3D malloc (sizeof (struct addrinfo) + socklen); - if (ai =3D=3D NULL) - { - free ((char *) canon); - result =3D -EAI_MEMORY; - goto free_and_return; - } - - ai->ai_flags =3D req->ai_flags; - ai->ai_family =3D family; - ai->ai_socktype =3D st2->socktype; - ai->ai_protocol =3D st2->protocol; - ai->ai_addrlen =3D socklen; - ai->ai_addr =3D (void *) (ai + 1); - - /* We only add the canonical name once. */ - ai->ai_canonname =3D (char *) canon; - canon =3D NULL; - -#ifdef _HAVE_SA_LEN - ai->ai_addr->sa_len =3D socklen; -#endif /* _HAVE_SA_LEN */ - ai->ai_addr->sa_family =3D family; - - /* In case of an allocation error the list must be NULL - terminated. */ - ai->ai_next =3D NULL; - - if (family =3D=3D AF_INET6) - { - struct sockaddr_in6 *sin6p =3D - (struct sockaddr_in6 *) ai->ai_addr; - - sin6p->sin6_port =3D st2->port; - sin6p->sin6_flowinfo =3D 0; - memcpy (&sin6p->sin6_addr, - at2->addr, sizeof (struct in6_addr)); - sin6p->sin6_scope_id =3D at2->scopeid; - } - else - { - struct sockaddr_in *sinp =3D - (struct sockaddr_in *) ai->ai_addr; - sinp->sin_port =3D st2->port; - memcpy (&sinp->sin_addr, - at2->addr, sizeof (struct in_addr)); - memset (sinp->sin_zero, '\0', sizeof (sinp->sin_zero)); - } - - pai =3D &(ai->ai_next); - } - - ++*naddrs; - - ignore: - at2 =3D at2->next; - } - } +done: + if (result =3D=3D 0) + result =3D process_list (req, at, st, canon ?: orig_name, canonbuf != =3D NULL, + got_ipv6, pai, naddrs); =20 - free_and_return: if (malloc_name) free ((char *) name); free (addrmem); - free (canonbuf); + if (result !=3D 0) + free (canonbuf); =20 return result; } --=20 2.31.1