From patchwork Sun Mar 1 18:11:55 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 5405 Received: (qmail 112248 invoked by alias); 2 Mar 2015 09:26:41 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 112175 invoked by uid 89); 2 Mar 2015 09:26:40 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.1 required=5.0 tests=AWL, BAYES_50, DATE_IN_PAST_12_24, SPF_HELO_PASS, SPF_PASS, T_RP_MATCHES_RCVD autolearn=no version=3.3.2 X-HELO: mx1.redhat.com Message-Id: <64f872e421f719b66df009245f1464c0537f8840.1425285061.git.fweimer@redhat.com> In-Reply-To: References: From: Florian Weimer Date: Sun, 1 Mar 2015 19:11:55 +0100 Subject: [PATCH 18/25] gethostid (Linux variant): Switch to struct scratch_buffer To: libc-alpha@sourceware.org Previously, extend_alloca was used without alloca accounting, which could have been problematic with large NSS results. --- sysdeps/unix/sysv/linux/gethostid.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/sysdeps/unix/sysv/linux/gethostid.c b/sysdeps/unix/sysv/linux/gethostid.c index 26e4692..512fb4c 100644 --- a/sysdeps/unix/sysv/linux/gethostid.c +++ b/sysdeps/unix/sysv/linux/gethostid.c @@ -64,13 +64,12 @@ sethostid (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; @@ -95,23 +94,26 @@ gethostid (void) /* This also fails. Return and arbitrary value. */ return 0; - buflen = 1024; - buffer = __alloca (buflen); + struct scratch_buffer tmpbuf; + scratch_buffer_init (&tmpbuf); /* To get the IP address we need to know the host name. */ - while (__gethostbyname_r (hostname, &hostbuf, buffer, buflen, &hp, &herr) - != 0 + while (__gethostbyname_r (hostname, &hostbuf, + tmpbuf.data, tmpbuf.length, &hp, &herr) != 0 || hp == NULL) if (herr != NETDB_INTERNAL || errno != ERANGE) - return 0; + { + scratch_buffer_free (&tmpbuf); + return 0; + } else - /* Enlarge buffer. */ - buffer = extend_alloca (buffer, buflen, 2 * buflen); + if (!scratch_buffer_grow (&tmpbuf)) + 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);