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 F0CEB3857C4A for ; Thu, 17 Mar 2022 04:20:28 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org F0CEB3857C4A Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-675-evQAVQ1ANMqrXJS3vsmz-Q-1; Thu, 17 Mar 2022 00:20:27 -0400 X-MC-Unique: evQAVQ1ANMqrXJS3vsmz-Q-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 EA08D185A7B2; Thu, 17 Mar 2022 04:20:26 +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 D63E840E80E1; Thu, 17 Mar 2022 04:20:26 +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 22H4KQ7b1744531; Thu, 17 Mar 2022 00:20:26 -0400 From: DJ Delorie To: Siddhesh Poyarekar Cc: libc-alpha@sourceware.org Subject: Re: [PATCH v2 06/12] gaih_inet: Split simple gethostbyname into its own function In-Reply-To: <20220314094835.1159523-7-siddhesh@sourceware.org> (message from Siddhesh Poyarekar via Libc-alpha on Mon, 14 Mar 2022 15:18:29 +0530) Date: Thu, 17 Mar 2022 00:20:26 -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.1 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=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: Thu, 17 Mar 2022 04:20:30 -0000 Siddhesh Poyarekar via Libc-alpha writes: > Add a free_at flag in gaih_result to indicate if res.at needs to be > freed by the caller. LGTM with one comment to be added. Reviewed-by: DJ Delorie > diff --git a/sysdeps/posix/getaddrinfo.c b/sysdeps/posix/getaddrinfo.c > index d7b6eae9fc..bcceab7d07 100644 > --- a/sysdeps/posix/getaddrinfo.c > +++ b/sysdeps/posix/getaddrinfo.c > @@ -120,6 +120,7 @@ struct gaih_result > { > struct gaih_addrtuple *at; > char *canon; > + bool free_at; > }; Ok. > @@ -565,6 +566,60 @@ out: > return result; > } > > +/* If possible, call the simple, old functions, which do not support IPv6 scope > + ids, nor retrieving the canonical name. */ > + > +static int > +try_simple_gethostbyname (const char *name, const struct addrinfo *req, > + struct scratch_buffer *tmpbuf, > + struct gaih_result *res) > +{ > + res->at = NULL; > + > + if (req->ai_family != AF_INET || (req->ai_flags & AI_CANONNAME) != 0) > + return 0; > + > + int rc; > + struct hostent th; > + struct hostent *h; > + > + while (1) > + { > + rc = __gethostbyname2_r (name, AF_INET, &th, tmpbuf->data, > + tmpbuf->length, &h, &h_errno); > + if (rc != ERANGE || h_errno != NETDB_INTERNAL) > + break; > + if (!scratch_buffer_grow (tmpbuf)) > + return -EAI_MEMORY; > + } > + > + if (rc == 0) > + { > + if (h != NULL) > + { > + /* We found data, convert it. */ > + if (!convert_hostent_to_gaih_addrtuple (req, AF_INET, h, &res->at)) > + return -EAI_MEMORY; > + > + res->free_at = true; Please add a comment here that res->at will either be the result of a realloc() or will be NULL, either of which may be safely passed to free(). convert_hostent_to_gaih_addrtuple() has a true return path that doesn't include the allocation, which may confuse future readers. Otherwise ok > + return 0; > + } > + if (h_errno == NO_DATA) > + return -EAI_NODATA; > + > + return -EAI_NONAME; > + } > + > + if (h_errno == NETDB_INTERNAL) > + return -EAI_SYSTEM; > + if (h_errno == TRY_AGAIN) > + return -EAI_AGAIN; > + > + /* We made requests but they turned out no data. > + The name is known, though. */ > + return -EAI_NODATA; > +} Ok. > static int > gaih_inet (const char *name, const struct gaih_service *service, > const struct addrinfo *req, struct addrinfo **pai, > @@ -610,6 +665,11 @@ gaih_inet (const char *name, const struct gaih_service *service, > else if (res.at != NULL) > goto process_list; > > + if ((result = try_simple_gethostbyname (name, req, tmpbuf, &res)) != 0) > + goto free_and_return; > + else if (res.at != NULL) > + goto process_list; > + Ok. > @@ -619,69 +679,6 @@ gaih_inet (const char *name, const struct gaih_service *service, > struct resolv_context *res_ctx = NULL; > bool do_merge = false; > > - /* If we do not have to look for IPv6 addresses or the canonical > - name, use the simple, old functions, which do not support > - . . . > - > - goto process_list; > - } Ok. > + if (res.free_at) > + free (res.at); Ok.