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 25FF73858438 for ; Tue, 8 Mar 2022 04:12:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 25FF73858438 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-19-8Gu3L_1JM_CbKUItBkatIw-1; Mon, 07 Mar 2022 23:12:28 -0500 X-MC-Unique: 8Gu3L_1JM_CbKUItBkatIw-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 8C9B48DCC46; Tue, 8 Mar 2022 04:12:27 +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 5631945308; Tue, 8 Mar 2022 04:12:27 +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 2284CQJ3277440; Mon, 7 Mar 2022 23:12:26 -0500 From: DJ Delorie To: Adhemerval Zanella Cc: libc-alpha@sourceware.org Subject: Re: [PATCH v2 3/3] inet: Return EAI_MEMORY when nrl_domainname() fails to allocate memory In-Reply-To: <20211210110733.1499984-4-adhemerval.zanella@linaro.org> (message from Adhemerval Zanella via Libc-alpha on Fri, 10 Dec 2021 08:07:33 -0300) Date: Mon, 07 Mar 2022 23:12:26 -0500 Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain X-Spam-Status: No, score=-5.9 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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-List-Received-Date: Tue, 08 Mar 2022 04:12:33 -0000 Adhemerval Zanella via Libc-alpha writes: > -static void > +static bool Ok. Please include comment saying what true/false return means, though. > nrl_domainname_core (struct scratch_buffer *tmpbuf) > { > char *c; > struct hostent *h, th; > int herror; > > - while (__gethostbyname_r ("localhost", &th, > - tmpbuf->data, tmpbuf->length, > + while (__gethostbyname_r ("localhost", &th, tmpbuf->data, tmpbuf->length, Formatting; ok > if (!scratch_buffer_grow (tmpbuf)) > - return; > + return false; Ok. > else > break; > } > > if (h != NULL && (c = strchr (h->h_name, '.')) != NULL) > - domain = __strdup (++c); > - else > { > + domain = __strdup (++c); > + return domain != NULL; > + } Ok. > - /* The name contains no domain information. Use the name > - now to get more information. */ > - while (__gethostname (tmpbuf->data, tmpbuf->length)) > - if (!scratch_buffer_grow (tmpbuf)) > - return; > + /* The name contains no domain information. Use the name > + now to get more information. */ > + while (__gethostname (tmpbuf->data, tmpbuf->length)) > + if (!scratch_buffer_grow (tmpbuf)) > + return false; Ok. > - > - if ((c = strchr (tmpbuf->data, '.')) != NULL) > - domain = __strdup (++c); > + if ((c = strchr (tmpbuf->data, '.')) != NULL) > + { > + domain = __strdup (++c); > + return domain != NULL; > + } Ok. > - else > - { > - /* We need to preserve the hostname. */ > - size_t hstnamelen = strlen (tmpbuf->data) + 1; > - while (__gethostbyname_r (tmpbuf->data, &th, > - tmpbuf->data + hstnamelen, > - tmpbuf->length - hstnamelen, > - &h, &herror)) > - { > - if (herror == NETDB_INTERNAL && errno == ERANGE) > - { > - if (!scratch_buffer_grow_preserve (tmpbuf)) > - return; > - } > - else > - break; > - } > > + /* We need to preserve the hostname. */ > + size_t hstnamelen = strlen (tmpbuf->data) + 1; > + while (__gethostbyname_r (tmpbuf->data, &th, tmpbuf->data + hstnamelen, > + tmpbuf->length - hstnamelen, &h, &herror)) > + { > + if (herror == NETDB_INTERNAL && errno == ERANGE) > + { > + if (!scratch_buffer_grow_preserve (tmpbuf)) > + return false; > + } > + else > + break; > + } Ok. > - if (h != NULL && (c = strchr(h->h_name, '.')) != NULL) > - domain = __strdup (++c); > + if (h != NULL && (c = strchr(h->h_name, '.')) != NULL) > + { > + domain = __strdup (++c); > + return domain != NULL; > + } Ok. > - else > - { > - struct in_addr in_addr; > > - in_addr.s_addr = htonl (INADDR_LOOPBACK); > > - while (__gethostbyaddr_r ((const char *) &in_addr, > - sizeof (struct in_addr), > - AF_INET, &th, > - tmpbuf->data, > - tmpbuf->length, > - &h, &herror)) > - { > - if (herror == NETDB_INTERNAL && errno == ERANGE) > - { > - if (!scratch_buffer_grow (tmpbuf)) > - return; > - } > - else > - break; > - } > > - if (h != NULL && (c = strchr (h->h_name, '.')) != NULL) > - domain = __strdup (++c); > - } > + > + struct in_addr in_addr = { .s_addr = htonl (INADDR_LOOPBACK) }; > + > + while (__gethostbyaddr_r ((const char *) &in_addr, sizeof (struct in_addr), > + AF_INET, &th, tmpbuf->data, tmpbuf->length, &h, > + &herror)) > + { > + if (herror == NETDB_INTERNAL && errno == ERANGE) > + { > + if (!scratch_buffer_grow (tmpbuf)) > + return false; > } > + else > + break; > + } > + > + if (h != NULL && (c = strchr (h->h_name, '.')) != NULL) > + { > + domain = __strdup (++c); > + return domain != NULL; > } > + return true; > } Ok. > -static char * > +static bool > nrl_domainname (void) > { > static int not_first; > > if (__glibc_likely (atomic_load_acquire (¬_first) != 0)) > - return domain; > + return true; If we successfully ran before, shortcut to success again. > + > + int r = true; Assume we'll succeed again, if more than one thread gets here. > __libc_lock_define_initialized (static, lock); > __libc_lock_lock (lock); > @@ -180,16 +181,15 @@ nrl_domainname (void) > struct scratch_buffer tmpbuf; > scratch_buffer_init (&tmpbuf); > > - nrl_domainname_core (&tmpbuf); > + if ((r = nrl_domainname_core (&tmpbuf))) > + atomic_store_release (¬_first, 1); If the call is successful, we set not_first to enable the above shortcut, and set r, which we return. I note that a failing first time through will not set not_first, so other threads contending for this lock will also call this block, and continue calling this block, until they succeed. Therefor the above shortcut's assumption is valid. Ok. > scratch_buffer_free (&tmpbuf); > - > - atomic_store_release (¬_first, 1); > } > > __libc_lock_unlock (lock); > > - return domain; > + return r; This will be TRUE if we skip the block because a previous thread succeeded, or FALSE if we ran the block and failed. > }; Ok. > /* Copy a string to a destination buffer with length checking. Return > @@ -285,13 +285,17 @@ gni_host_inet_name (struct scratch_buffer *tmpbuf, > > if (h) > { > - char *c; > - if ((flags & NI_NOFQDN) > - && (c = nrl_domainname ()) > - && (c = strstr (h->h_name, c)) > - && (c != h->h_name) && (*(--c) == '.')) > - /* Terminate the string after the prefix. */ > - *c = '\0'; > + if (flags & NI_NOFQDN) > + { > + if (!nrl_domainname ()) > + return EAI_MEMORY; Same as first line in above logic; ok. > + char *c = domain; > + if (c != NULL && (c = strstr (h->h_name, c)) > + && (c != h->h_name) && (*(--c) == '.')) Matches remainder of the logic. Ok. > + /* Terminate the string after the prefix. */ > + *c = '\0'; > + } Ok. LGTM with that one comment added. Reviewed-by: DJ Delorie