From patchwork Tue Aug 3 21:29:15 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Siddhesh Poyarekar X-Patchwork-Id: 1513098 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=QtYa7gXK; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (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 ozlabs.org (Postfix) with ESMTPS id 4GfShv44Bkz9sRf for ; Wed, 4 Aug 2021 07:30:39 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 56F223858C27 for ; Tue, 3 Aug 2021 21:30:37 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 56F223858C27 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1628026237; bh=SbkLEiIYQNvbuitWbLR7pM2NImGOvC57Tte1LIaWgUY=; 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=QtYa7gXKglrxaofvAbHTlOMA8A8njmD9tVIMw0JVO64jugcdWkb69mpMknKuBG8l2 QGnubTUoCmQQTv98vZmeJhRUBCZVTVikgJ0HzyHEAKWiCKLz6Mm9zXCp7S92mGAQsE 1ny4NMc2xYG40jicut75UxlydvzY6LjU6gjsYxn4= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from dormouse.elm.relay.mailchannels.net (dormouse.elm.relay.mailchannels.net [23.83.212.50]) by sourceware.org (Postfix) with ESMTPS id ADF5A3858C27 for ; Tue, 3 Aug 2021 21:29:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org ADF5A3858C27 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 40A45101B12 for ; Tue, 3 Aug 2021 21:29:32 +0000 (UTC) Received: from pdx1-sub0-mail-a4.g.dreamhost.com (100-105-161-178.trex.outbound.svc.cluster.local [100.105.161.178]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id B6B9A101C06 for ; Tue, 3 Aug 2021 21:29:31 +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.105.161.178 (trex/6.3.3); Tue, 03 Aug 2021 21:29:32 +0000 X-MC-Relay: Neutral X-MailChannels-SenderId: dreamhost|x-authsender|siddhesh@gotplt.org X-MailChannels-Auth-Id: dreamhost X-Desert-Vacuous: 636462b06a14624f_1628026171977_1102994129 X-MC-Loop-Signature: 1628026171977:4279582436 X-MC-Ingress-Time: 1628026171977 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 57F858AAA8 for ; Tue, 3 Aug 2021 14:29:31 -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 304A58AAAA for ; Tue, 3 Aug 2021 14:29:29 -0700 (PDT) X-DH-BACKEND: pdx1-sub0-mail-a4 To: libc-alpha@sourceware.org Subject: [PATCH 1/5] gaiconf_init: Refactor some bits for readability Date: Wed, 4 Aug 2021 02:59:15 +0530 Message-Id: <20210803212919.3059194-2-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 X-Spam-Status: No, score=-3494.0 required=5.0 tests=BAYES_00, GIT_PATCH_0, JMQ_SPF_NEUTRAL, KAM_DMARC_NONE, KAM_DMARC_STATUS, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NEUTRAL, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org 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: , X-Patchwork-Original-From: Siddhesh Poyarekar via Libc-alpha From: Siddhesh Poyarekar Reply-To: Siddhesh Poyarekar Errors-To: libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org Sender: "Libc-alpha" Split out line processing for `label`, `precedence` and `scopev4` into separate functions instead of the gotos. --- sysdeps/posix/getaddrinfo.c | 149 ++++++++++++++++++++---------------- 1 file changed, 84 insertions(+), 65 deletions(-) diff --git a/sysdeps/posix/getaddrinfo.c b/sysdeps/posix/getaddrinfo.c index 9f1cde27b5..f222c2a62f 100644 --- a/sysdeps/posix/getaddrinfo.c +++ b/sysdeps/posix/getaddrinfo.c @@ -1761,6 +1761,66 @@ scopecmp (const void *p1, const void *p2) return 1; } +static bool +add_prefixlist (struct prefixlist **listp, size_t *lenp, bool *nullbitsp, + char *val1, char *val2, char **pos) +{ + struct in6_addr prefix; + unsigned long int bits; + unsigned long int val; + char *endp; + + bits = 128; + __set_errno (0); + char *cp = strchr (val1, '/'); + if (cp != NULL) + *cp++ = '\0'; + *pos = cp; + if (inet_pton (AF_INET6, val1, &prefix) + && (cp == NULL + || (bits = strtoul (cp, &endp, 10)) != ULONG_MAX + || errno != ERANGE) + && *endp == '\0' + && bits <= 128 + && ((val = strtoul (val2, &endp, 10)) != ULONG_MAX + || errno != ERANGE) + && *endp == '\0' + && val <= INT_MAX) + { + struct prefixlist *newp = malloc (sizeof (*newp)); + if (newp == NULL) + return false; + + memcpy (&newp->entry.prefix, &prefix, sizeof (prefix)); + newp->entry.bits = bits; + newp->entry.val = val; + newp->next = *listp; + *listp = newp; + ++*lenp; + *nullbitsp |= bits == 0; + } + return true; +} + +static bool +add_scopelist (struct scopelist **listp, size_t *lenp, bool *nullbitsp, + const struct in6_addr *prefixp, unsigned long int bits, + unsigned long int val) +{ + struct scopelist *newp = malloc (sizeof (*newp)); + if (newp == NULL) + return false; + + newp->entry.netmask = htonl (bits != 96 ? (0xffffffff << (128 - bits)) : 0); + newp->entry.addr32 = (prefixp->s6_addr32[3] & newp->entry.netmask); + newp->entry.scope = val; + newp->next = *listp; + *listp = newp; + ++*lenp; + *nullbitsp |= bits == 96; + + return true; +} static void gaiconf_init (void) @@ -1836,55 +1896,17 @@ gaiconf_init (void) /* Ignore the rest of the line. */ *cp = '\0'; - struct prefixlist **listp; - size_t *lenp; - bool *nullbitsp; switch (cmdlen) { case 5: if (strcmp (cmd, "label") == 0) { - struct in6_addr prefix; - unsigned long int bits; - unsigned long int val; - char *endp; - - listp = &labellist; - lenp = &nlabellist; - nullbitsp = &labellist_nullbits; - - new_elem: - bits = 128; - __set_errno (0); - cp = strchr (val1, '/'); - if (cp != NULL) - *cp++ = '\0'; - if (inet_pton (AF_INET6, val1, &prefix) - && (cp == NULL - || (bits = strtoul (cp, &endp, 10)) != ULONG_MAX - || errno != ERANGE) - && *endp == '\0' - && bits <= 128 - && ((val = strtoul (val2, &endp, 10)) != ULONG_MAX - || errno != ERANGE) - && *endp == '\0' - && val <= INT_MAX) + if (!add_prefixlist (&labellist, &nlabellist, + &labellist_nullbits, val1, val2, &cp)) { - struct prefixlist *newp = malloc (sizeof (*newp)); - if (newp == NULL) - { - free (line); - fclose (fp); - goto no_file; - } - - memcpy (&newp->entry.prefix, &prefix, sizeof (prefix)); - newp->entry.bits = bits; - newp->entry.val = val; - newp->next = *listp; - *listp = newp; - ++*lenp; - *nullbitsp |= bits == 0; + free (line); + fclose (fp); + goto no_file; } } break; @@ -1926,27 +1948,14 @@ gaiconf_init (void) && *endp == '\0' && val <= INT_MAX) { - struct scopelist *newp; - new_scope: - newp = malloc (sizeof (*newp)); - if (newp == NULL) + if (!add_scopelist (&scopelist, &nscopelist, + &scopelist_nullbits, &prefix, + bits, val)) { free (line); fclose (fp); goto no_file; } - - newp->entry.netmask = htonl (bits != 96 - ? (0xffffffff - << (128 - bits)) - : 0); - newp->entry.addr32 = (prefix.s6_addr32[3] - & newp->entry.netmask); - newp->entry.scope = val; - newp->next = scopelist; - scopelist = newp; - ++nscopelist; - scopelist_nullbits |= bits == 96; } } else if (inet_pton (AF_INET, val1, &prefix.s6_addr32[3]) @@ -1960,8 +1969,14 @@ gaiconf_init (void) && *endp == '\0' && val <= INT_MAX) { - bits += 96; - goto new_scope; + if (!add_scopelist (&scopelist, &nscopelist, + &scopelist_nullbits, &prefix, + bits + 96, val)) + { + free (line); + fclose (fp); + goto no_file; + } } } break; @@ -1969,10 +1984,14 @@ gaiconf_init (void) case 10: if (strcmp (cmd, "precedence") == 0) { - listp = &precedencelist; - lenp = &nprecedencelist; - nullbitsp = &precedencelist_nullbits; - goto new_elem; + if (!add_prefixlist (&precedencelist, &nprecedencelist, + &precedencelist_nullbits, val1, val2, + &cp)) + { + free (line); + fclose (fp); + goto no_file; + } } break; }