From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 83282 invoked by alias); 26 Jun 2018 16:58:19 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Received: (qmail 83268 invoked by uid 89); 26 Jun 2018 16:58:18 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-25.4 required=5.0 tests=AWL,BAYES_00,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3,RCVD_IN_DNSWL_NONE,SPF_PASS autolearn=ham version=3.3.2 spammy= X-HELO: mail-qk0-f193.google.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=subject:to:references:from:openpgp:autocrypt:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=lILNs3v4ZSAN8r37xhumNTytufZHbjXtGMzK6qHq+BQ=; b=eI1+kBJy4l/9OzyQkQdYc7598iQ1G6zaOVfQ7b4lGCGIj8YZNWsKUmOIv/qB9iaY0s O0bAnSfDGjZhMK+KhdI5qANCryzrHjR2I32ZoDgxY+Sb+ookoeU+3QCdJFRMETulrOmz MoL9WBgYbxcMOxJyJxef+c0Gcqu/j9HPc8FO0= Return-Path: Subject: Re: [PATCH] gethostid (Linux variant): Switch to struct scratch_buffer [BZ #18023] To: libc-alpha@sourceware.org References: <20180626154633.BC8B943994575@oldenburg.str.redhat.com> From: Adhemerval Zanella Openpgp: preference=signencrypt Message-ID: Date: Tue, 26 Jun 2018 16:58:00 -0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.8.0 MIME-Version: 1.0 In-Reply-To: <20180626154633.BC8B943994575@oldenburg.str.redhat.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-SW-Source: 2018-06/txt/msg00809.txt.bz2 On 26/06/2018 12:46, Florian Weimer wrote: > Previously, extend_alloca was used without alloca accounting, > which could have been problematic with large NSS results. > > 2018-06-26 Florian Weimer > > [BZ #18023] > * sysdeps/unix/sysv/linux/gethostid.c (gethostid): Use struct > scratch_buffer instead of extend_alloca. Update comments. > > diff --git a/sysdeps/unix/sysv/linux/gethostid.c b/sysdeps/unix/sysv/linux/gethostid.c > index 73c56e57e5..c8a33fe14d 100644 > --- a/sysdeps/unix/sysv/linux/gethostid.c > +++ b/sysdeps/unix/sysv/linux/gethostid.c > @@ -21,6 +21,7 @@ > #include > #include > #include > +#include > > #define HOSTIDFILE "/etc/hostid" > > @@ -63,13 +64,12 @@ sethostid (long int id) > # include > # include > # include > +# include > > long int > gethostid (void) > { > char hostname[MAXHOSTNAMELEN + 1]; > - size_t buflen; > - char *buffer; > struct hostent hostbuf, *hp; > int32_t id; > struct in_addr in; > @@ -88,29 +88,40 @@ gethostid (void) > return id; > } > > - /* Getting from the file was not successful. An intelligent guess for > - a unique number of a host is its IP address. Return this. */ > + /* Getting from the file was not successful. An intelligent guess > + for a unique number of a host is its IP address. To get the IP > + address we need to know the host name. */ > if (__gethostname (hostname, MAXHOSTNAMELEN) < 0 || hostname[0] == '\0') > /* This also fails. Return and arbitrary value. */ > return 0; > > - buflen = 1024; > - buffer = __alloca (buflen); > - > - /* To get the IP address we need to know the host name. */ > - while (__gethostbyname_r (hostname, &hostbuf, buffer, buflen, &hp, &herr) > - != 0 > - || hp == NULL) > - if (herr != NETDB_INTERNAL || errno != ERANGE) > - return 0; > - else > - /* Enlarge buffer. */ > - buffer = extend_alloca (buffer, buflen, 2 * buflen); > + /* Determine the IP address of the host name. */ > + struct scratch_buffer tmpbuf; > + scratch_buffer_init (&tmpbuf); > + while (true) > + { > + int ret = __gethostbyname_r (hostname, &hostbuf, > + tmpbuf.data, tmpbuf.length, &hp, &herr); > + if (ret == 0) > + break; > + else > + { > + /* Enlarge the buffer on ERANGE. */ > + if (herr == NETDB_INTERNAL && errno == ERANGE) > + { > + if (!scratch_buffer_grow (&tmpbuf)) > + return 0; > + } > + else > + /* Other errors are a failure. Return an arbitrary value. */ Shouldn' it call 'scratch_buffer_free' here for the case the buffer is grown and a subsequent __gethostbyname_r results something different than ERANGE (assuming it is possible)? > + return 0; > + } > + } > > in.s_addr = 0; > memcpy (&in, hp->h_addr, > (int) sizeof (in) < hp->h_length ? (int) sizeof (in) : hp->h_length); > - > + scratch_buffer_free (&tmpbuf); > /* For the return value to be not exactly the IP address we do some > bit fiddling. */ > return (int32_t) (in.s_addr << 16 | in.s_addr >> 16); >