From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 341333876053 for ; Thu, 17 Mar 2022 22:41:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 341333876053 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-141-A4CeMTytOyO4NqfK1O0PMQ-1; Thu, 17 Mar 2022 18:41:42 -0400 X-MC-Unique: A4CeMTytOyO4NqfK1O0PMQ-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 72583299E763; Thu, 17 Mar 2022 22:41:42 +0000 (UTC) Received: from greed.delorie.com (ovpn-112-4.rdu2.redhat.com [10.10.112.4]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 5E1C040FF40F; Thu, 17 Mar 2022 22:41:42 +0000 (UTC) Received: from greed.delorie.com.redhat.com (localhost [127.0.0.1]) by greed.delorie.com (8.15.2/8.15.2) with ESMTP id 22HMffe61763554; Thu, 17 Mar 2022 18:41:41 -0400 From: DJ Delorie To: Siddhesh Poyarekar Cc: libc-alpha@sourceware.org Subject: Re: [PATCH 1/3] gaiconf_init: Refactor some bits for readability In-Reply-To: <20220314173039.1060650-2-siddhesh@sourceware.org> (message from Siddhesh Poyarekar via Libc-alpha on Mon, 14 Mar 2022 23:00:37 +0530) Date: Thu, 17 Mar 2022 18:41:41 -0400 Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.11.54.2 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain X-Spam-Status: No, score=-11.9 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=unavailable 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-List-Received-Date: Thu, 17 Mar 2022 22:41:47 -0000 Siddhesh Poyarekar via Libc-alpha writes: > Split out line processing for `label`, `precedence` and `scopev4` into > separate functions instead of the gotos. LGTM. Reviewed-by: DJ Delorie > diff --git a/sysdeps/posix/getaddrinfo.c b/sysdeps/posix/getaddrinfo.c > +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; > +} Matches removed code, ok. > +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; > +} matches removed code, ok. > 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) > { > + if (!add_prefixlist (&labellist, &nlabellist, > + &labellist_nullbits, val1, val2, &cp)) > { > + free (line); > + fclose (fp); > + goto no_file; > } Ok. > - 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) > { > - 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; Matches extracted code, ok. > } > } > break; > @@ -1926,27 +1948,14 @@ gaiconf_init (void) > && *endp == '\0' > && val <= INT_MAX) > { > + if (!add_scopelist (&scopelist, &nscopelist, > + &scopelist_nullbits, &prefix, > + bits, val)) Ok. > { > free (line); > fclose (fp); > goto no_file; > } > - struct scopelist *newp; > - new_scope: > - newp = malloc (sizeof (*newp)); > - if (newp == NULL) > - ... > - > - 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; Matches extracted code, ok. > } > 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; > + } Ok. > @@ -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; > + } Ok.