From patchwork Tue Aug 3 21:29:16 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Siddhesh Poyarekar X-Patchwork-Id: 44563 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 240F7396DC2F for ; Tue, 3 Aug 2021 21:32:22 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 240F7396DC2F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1628026342; bh=94Gt02XKY9yrmUT2KTAI91bgICC63a/mJM3rjN8jpqc=; 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=fH45VzUBFr0fCdHj3H4bpEUh0KJDl0Id050ioEEkFlJEXOUu+PtcW4HgQlztIg57c OyZtd8bBA8MaqWeZGtbtRCIC8ckmM37u0LTZYzbs29ON2OrhuSETjNjaYpyEML/FeG PAhv8tcTNw+eDuk2odwlnsGC8s+BmWalY0CebWk8= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from bumble.birch.relay.mailchannels.net (bumble.birch.relay.mailchannels.net [23.83.209.25]) by sourceware.org (Postfix) with ESMTPS id A0B81385800F for ; Tue, 3 Aug 2021 21:29:35 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org A0B81385800F 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 35CC75A15E5 for ; Tue, 3 Aug 2021 21:29:34 +0000 (UTC) Received: from pdx1-sub0-mail-a4.g.dreamhost.com (100-101-162-78.trex-nlb.outbound.svc.cluster.local [100.101.162.78]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id C52A65A17E2 for ; Tue, 3 Aug 2021 21:29:33 +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.101.162.78 (trex/6.3.3); Tue, 03 Aug 2021 21:29:34 +0000 X-MC-Relay: Neutral X-MailChannels-SenderId: dreamhost|x-authsender|siddhesh@gotplt.org X-MailChannels-Auth-Id: dreamhost X-Spot-Plucky: 72ae9f4951c939cb_1628026174034_3392630672 X-MC-Loop-Signature: 1628026174034:1654015545 X-MC-Ingress-Time: 1628026174034 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 541D98AAB1 for ; Tue, 3 Aug 2021 14:29:33 -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 14EDA8AAB3 for ; Tue, 3 Aug 2021 14:29:31 -0700 (PDT) X-DH-BACKEND: pdx1-sub0-mail-a4 To: libc-alpha@sourceware.org Subject: [PATCH 2/5] gai_init: Avoid jumping from if condition to its else counterpart Date: Wed, 4 Aug 2021 02:59:16 +0530 Message-Id: <20210803212919.3059194-3-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_H4, RCVD_IN_MSPIKE_WL, 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+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" Clean up another antipattern where code flows from an if condition to its else counterpart with a goto. Most of the change in this patch is whitespace-only; a `git diff -b` ought to show the actual logic changes. --- sysdeps/posix/getaddrinfo.c | 516 ++++++++++++++++++------------------ 1 file changed, 257 insertions(+), 259 deletions(-) diff --git a/sysdeps/posix/getaddrinfo.c b/sysdeps/posix/getaddrinfo.c index f222c2a62f..180048eec2 100644 --- a/sysdeps/posix/getaddrinfo.c +++ b/sysdeps/posix/getaddrinfo.c @@ -1836,142 +1836,122 @@ gaiconf_init (void) bool scopelist_nullbits = false; FILE *fp = fopen (GAICONF_FNAME, "rce"); - if (fp != NULL) + if (fp == NULL) + goto no_file; + + struct __stat64_t64 st; + if (__fstat64_time64 (fileno (fp), &st) != 0) { - struct __stat64_t64 st; - if (__fstat64_time64 (fileno (fp), &st) != 0) - { - fclose (fp); - goto no_file; - } + fclose (fp); + goto no_file; + } - char *line = NULL; - size_t linelen = 0; + char *line = NULL; + size_t linelen = 0; - __fsetlocking (fp, FSETLOCKING_BYCALLER); + __fsetlocking (fp, FSETLOCKING_BYCALLER); - while (!feof_unlocked (fp)) + while (!feof_unlocked (fp)) + { + ssize_t n = __getline (&line, &linelen, fp); + if (n <= 0) + break; + + /* Handle comments. No escaping possible so this is easy. */ + char *cp = strchr (line, '#'); + if (cp != NULL) + *cp = '\0'; + + cp = line; + while (isspace (*cp)) + ++cp; + + char *cmd = cp; + while (*cp != '\0' && !isspace (*cp)) + ++cp; + size_t cmdlen = cp - cmd; + + if (*cp != '\0') + *cp++ = '\0'; + while (isspace (*cp)) + ++cp; + + char *val1 = cp; + while (*cp != '\0' && !isspace (*cp)) + ++cp; + size_t val1len = cp - cmd; + + /* We always need at least two values. */ + if (val1len == 0) + continue; + + if (*cp != '\0') + *cp++ = '\0'; + while (isspace (*cp)) + ++cp; + + char *val2 = cp; + while (*cp != '\0' && !isspace (*cp)) + ++cp; + + /* Ignore the rest of the line. */ + *cp = '\0'; + + switch (cmdlen) { - ssize_t n = __getline (&line, &linelen, fp); - if (n <= 0) - break; - - /* Handle comments. No escaping possible so this is easy. */ - char *cp = strchr (line, '#'); - if (cp != NULL) - *cp = '\0'; - - cp = line; - while (isspace (*cp)) - ++cp; - - char *cmd = cp; - while (*cp != '\0' && !isspace (*cp)) - ++cp; - size_t cmdlen = cp - cmd; - - if (*cp != '\0') - *cp++ = '\0'; - while (isspace (*cp)) - ++cp; - - char *val1 = cp; - while (*cp != '\0' && !isspace (*cp)) - ++cp; - size_t val1len = cp - cmd; - - /* We always need at least two values. */ - if (val1len == 0) - continue; - - if (*cp != '\0') - *cp++ = '\0'; - while (isspace (*cp)) - ++cp; - - char *val2 = cp; - while (*cp != '\0' && !isspace (*cp)) - ++cp; - - /* Ignore the rest of the line. */ - *cp = '\0'; - - switch (cmdlen) + case 5: + if (strcmp (cmd, "label") == 0) { - case 5: - if (strcmp (cmd, "label") == 0) + if (!add_prefixlist (&labellist, &nlabellist, + &labellist_nullbits, val1, val2, &cp)) { - if (!add_prefixlist (&labellist, &nlabellist, - &labellist_nullbits, val1, val2, &cp)) - { - free (line); - fclose (fp); - goto no_file; - } + free (line); + fclose (fp); + goto no_file; } - break; + } + break; - case 6: - if (strcmp (cmd, "reload") == 0) - { - gaiconf_reload_flag = strcmp (val1, "yes") == 0; - if (gaiconf_reload_flag) - gaiconf_reload_flag_ever_set = 1; - } - break; + case 6: + if (strcmp (cmd, "reload") == 0) + { + gaiconf_reload_flag = strcmp (val1, "yes") == 0; + if (gaiconf_reload_flag) + gaiconf_reload_flag_ever_set = 1; + } + break; - case 7: - if (strcmp (cmd, "scopev4") == 0) + case 7: + if (strcmp (cmd, "scopev4") == 0) + { + struct in6_addr prefix; + unsigned long int bits; + unsigned long int val; + char *endp; + + bits = 32; + __set_errno (0); + cp = strchr (val1, '/'); + if (cp != NULL) + *cp++ = '\0'; + if (inet_pton (AF_INET6, val1, &prefix)) { - struct in6_addr prefix; - unsigned long int bits; - unsigned long int val; - char *endp; - - bits = 32; - __set_errno (0); - cp = strchr (val1, '/'); - if (cp != NULL) - *cp++ = '\0'; - if (inet_pton (AF_INET6, val1, &prefix)) - { - bits = 128; - if (IN6_IS_ADDR_V4MAPPED (&prefix) - && (cp == NULL - || (bits = strtoul (cp, &endp, 10)) != ULONG_MAX - || errno != ERANGE) - && *endp == '\0' - && bits >= 96 - && bits <= 128 - && ((val = strtoul (val2, &endp, 10)) != ULONG_MAX - || errno != ERANGE) - && *endp == '\0' - && val <= INT_MAX) - { - if (!add_scopelist (&scopelist, &nscopelist, - &scopelist_nullbits, &prefix, - bits, val)) - { - free (line); - fclose (fp); - goto no_file; - } - } - } - else if (inet_pton (AF_INET, val1, &prefix.s6_addr32[3]) - && (cp == NULL - || (bits = strtoul (cp, &endp, 10)) != ULONG_MAX - || errno != ERANGE) - && *endp == '\0' - && bits <= 32 - && ((val = strtoul (val2, &endp, 10)) != ULONG_MAX - || errno != ERANGE) - && *endp == '\0' - && val <= INT_MAX) + bits = 128; + if (IN6_IS_ADDR_V4MAPPED (&prefix) + && (cp == NULL + || (bits = strtoul (cp, &endp, 10)) != ULONG_MAX + || errno != ERANGE) + && *endp == '\0' + && bits >= 96 + && bits <= 128 + && ((val = strtoul (val2, &endp, 10)) != ULONG_MAX + || errno != ERANGE) + && *endp == '\0' + && val <= INT_MAX) { if (!add_scopelist (&scopelist, &nscopelist, &scopelist_nullbits, &prefix, - bits + 96, val)) + bits, val)) { free (line); fclose (fp); @@ -1979,173 +1959,191 @@ gaiconf_init (void) } } } - break; - - case 10: - if (strcmp (cmd, "precedence") == 0) + else if (inet_pton (AF_INET, val1, &prefix.s6_addr32[3]) + && (cp == NULL + || (bits = strtoul (cp, &endp, 10)) != ULONG_MAX + || errno != ERANGE) + && *endp == '\0' + && bits <= 32 + && ((val = strtoul (val2, &endp, 10)) != ULONG_MAX + || errno != ERANGE) + && *endp == '\0' + && val <= INT_MAX) { - if (!add_prefixlist (&precedencelist, &nprecedencelist, - &precedencelist_nullbits, val1, val2, - &cp)) + if (!add_scopelist (&scopelist, &nscopelist, + &scopelist_nullbits, &prefix, + bits + 96, val)) { free (line); fclose (fp); goto no_file; } } - break; - } - } - - free (line); - - fclose (fp); - - /* Create the array for the labels. */ - struct prefixentry *new_labels; - if (nlabellist > 0) - { - if (!labellist_nullbits) - ++nlabellist; - new_labels = malloc (nlabellist * sizeof (*new_labels)); - if (new_labels == NULL) - goto no_file; - - int i = nlabellist; - if (!labellist_nullbits) - { - --i; - memset (&new_labels[i].prefix, '\0', sizeof (struct in6_addr)); - new_labels[i].bits = 0; - new_labels[i].val = 1; } + break; - struct prefixlist *l = labellist; - while (i-- > 0) + case 10: + if (strcmp (cmd, "precedence") == 0) { - new_labels[i] = l->entry; - l = l->next; + if (!add_prefixlist (&precedencelist, &nprecedencelist, + &precedencelist_nullbits, val1, val2, + &cp)) + { + free (line); + fclose (fp); + goto no_file; + } } - free_prefixlist (labellist); - labellist = NULL; - - /* Sort the entries so that the most specific ones are at - the beginning. */ - qsort (new_labels, nlabellist, sizeof (*new_labels), prefixcmp); + break; } - else - new_labels = (struct prefixentry *) default_labels; - - struct prefixentry *new_precedence; - if (nprecedencelist > 0) - { - if (!precedencelist_nullbits) - ++nprecedencelist; - new_precedence = malloc (nprecedencelist * sizeof (*new_precedence)); - if (new_precedence == NULL) - { - if (new_labels != default_labels) - free (new_labels); - goto no_file; - } + } - int i = nprecedencelist; - if (!precedencelist_nullbits) - { - --i; - memset (&new_precedence[i].prefix, '\0', - sizeof (struct in6_addr)); - new_precedence[i].bits = 0; - new_precedence[i].val = 40; - } + free (line); - struct prefixlist *l = precedencelist; - while (i-- > 0) - { - new_precedence[i] = l->entry; - l = l->next; - } - free_prefixlist (precedencelist); - precedencelist = NULL; + fclose (fp); - /* Sort the entries so that the most specific ones are at - the beginning. */ - qsort (new_precedence, nprecedencelist, sizeof (*new_precedence), - prefixcmp); + /* Create the array for the labels. */ + struct prefixentry *new_labels; + if (nlabellist > 0) + { + if (!labellist_nullbits) + ++nlabellist; + new_labels = malloc (nlabellist * sizeof (*new_labels)); + if (new_labels == NULL) + goto no_file; + + int i = nlabellist; + if (!labellist_nullbits) + { + --i; + memset (&new_labels[i].prefix, '\0', sizeof (struct in6_addr)); + new_labels[i].bits = 0; + new_labels[i].val = 1; } - else - new_precedence = (struct prefixentry *) default_precedence; - struct scopeentry *new_scopes; - if (nscopelist > 0) + struct prefixlist *l = labellist; + while (i-- > 0) { - if (!scopelist_nullbits) - ++nscopelist; - new_scopes = malloc (nscopelist * sizeof (*new_scopes)); - if (new_scopes == NULL) - { - if (new_labels != default_labels) - free (new_labels); - if (new_precedence != default_precedence) - free (new_precedence); - goto no_file; - } - - int i = nscopelist; - if (!scopelist_nullbits) - { - --i; - new_scopes[i].addr32 = 0; - new_scopes[i].netmask = 0; - new_scopes[i].scope = 14; - } + new_labels[i] = l->entry; + l = l->next; + } + free_prefixlist (labellist); + labellist = NULL; - struct scopelist *l = scopelist; - while (i-- > 0) - { - new_scopes[i] = l->entry; - l = l->next; - } - free_scopelist (scopelist); + /* Sort the entries so that the most specific ones are at + the beginning. */ + qsort (new_labels, nlabellist, sizeof (*new_labels), prefixcmp); + } + else + new_labels = (struct prefixentry *) default_labels; - /* Sort the entries so that the most specific ones are at - the beginning. */ - qsort (new_scopes, nscopelist, sizeof (*new_scopes), - scopecmp); + struct prefixentry *new_precedence; + if (nprecedencelist > 0) + { + if (!precedencelist_nullbits) + ++nprecedencelist; + new_precedence = malloc (nprecedencelist * sizeof (*new_precedence)); + if (new_precedence == NULL) + { + if (new_labels != default_labels) + free (new_labels); + goto no_file; } - else - new_scopes = (struct scopeentry *) default_scopes; - - /* Now we are ready to replace the values. */ - const struct prefixentry *old = labels; - labels = new_labels; - if (old != default_labels) - free ((void *) old); - old = precedence; - precedence = new_precedence; - if (old != default_precedence) - free ((void *) old); + int i = nprecedencelist; + if (!precedencelist_nullbits) + { + --i; + memset (&new_precedence[i].prefix, '\0', + sizeof (struct in6_addr)); + new_precedence[i].bits = 0; + new_precedence[i].val = 40; + } - const struct scopeentry *oldscope = scopes; - scopes = new_scopes; - if (oldscope != default_scopes) - free ((void *) oldscope); + struct prefixlist *l = precedencelist; + while (i-- > 0) + { + new_precedence[i] = l->entry; + l = l->next; + } + free_prefixlist (precedencelist); + precedencelist = NULL; - save_gaiconf_mtime (&st); + /* Sort the entries so that the most specific ones are at + the beginning. */ + qsort (new_precedence, nprecedencelist, sizeof (*new_precedence), + prefixcmp); } else + new_precedence = (struct prefixentry *) default_precedence; + + struct scopeentry *new_scopes; + if (nscopelist > 0) { - no_file: - free_prefixlist (labellist); - free_prefixlist (precedencelist); + if (!scopelist_nullbits) + ++nscopelist; + new_scopes = malloc (nscopelist * sizeof (*new_scopes)); + if (new_scopes == NULL) + { + if (new_labels != default_labels) + free (new_labels); + if (new_precedence != default_precedence) + free (new_precedence); + goto no_file; + } + + int i = nscopelist; + if (!scopelist_nullbits) + { + --i; + new_scopes[i].addr32 = 0; + new_scopes[i].netmask = 0; + new_scopes[i].scope = 14; + } + + struct scopelist *l = scopelist; + while (i-- > 0) + { + new_scopes[i] = l->entry; + l = l->next; + } free_scopelist (scopelist); - /* If we previously read the file but it is gone now, free the - old data and use the builtin one. Leave the reload flag - alone. */ - fini (); + /* Sort the entries so that the most specific ones are at + the beginning. */ + qsort (new_scopes, nscopelist, sizeof (*new_scopes), + scopecmp); } + else + new_scopes = (struct scopeentry *) default_scopes; + + /* Now we are ready to replace the values. */ + const struct prefixentry *old = labels; + labels = new_labels; + if (old != default_labels) + free ((void *) old); + + old = precedence; + precedence = new_precedence; + if (old != default_precedence) + free ((void *) old); + + const struct scopeentry *oldscope = scopes; + scopes = new_scopes; + if (oldscope != default_scopes) + free ((void *) oldscope); + + save_gaiconf_mtime (&st); + return; + +no_file: + free_prefixlist (labellist); + free_prefixlist (precedencelist); + free_scopelist (scopelist); + + /* If we previously read the file but it is gone now, free the old data and + use the builtin one. Leave the reload flag alone. */ + fini (); }