public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Florian Weimer <fweimer@redhat.com>
To: Adhemerval Zanella <adhemerval.zanella@linaro.org>,
	libc-alpha@sourceware.org
Subject: Re: [PATCH] gethostid (Linux variant): Switch to struct scratch_buffer [BZ #18023]
Date: Tue, 26 Jun 2018 17:33:00 -0000	[thread overview]
Message-ID: <df00e77d-528d-e410-ecb2-7699d45ebd5d@redhat.com> (raw)
In-Reply-To: <aa50fb15-99d8-28c3-d3ea-a883a8563634@linaro.org>

[-- Attachment #1: Type: text/plain, Size: 883 bytes --]

On 06/26/2018 06:58 PM, Adhemerval Zanella wrote:
>> +  /* 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)?

Thanks, you are right.  New patch attached.

Florian

[-- Attachment #2: gethostid.patch --]
[-- Type: text/x-patch, Size: 2963 bytes --]

Subject: [PATCH] gethostid (Linux variant): Switch to struct scratch_buffer [BZ #18023]
To: libc-alpha@sourceware.org

Previously, extend_alloca was used without alloca accounting,
which could have been problematic with large NSS results.

2018-06-26  Florian Weimer  <fweimer@redhat.com>

	[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..2e20f034dc 100644
--- a/sysdeps/unix/sysv/linux/gethostid.c
+++ b/sysdeps/unix/sysv/linux/gethostid.c
@@ -21,6 +21,7 @@
 #include <unistd.h>
 #include <netdb.h>
 #include <not-cancel.h>
+#include <stdbool.h>
 
 #define HOSTIDFILE "/etc/hostid"
 
@@ -63,13 +64,12 @@ sethostid (long int id)
 # include <sys/param.h>
 # include <resolv/netdb.h>
 # include <netinet/in.h>
+# include <scratch_buffer.h>
 
 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,43 @@ 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;
+	    }
+	  /* Other errors are a failure.  Return an arbitrary value.  */
+	  else
+	    {
+	      scratch_buffer_free (&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);

  reply	other threads:[~2018-06-26 17:33 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-06-26 15:46 Florian Weimer
2018-06-26 16:58 ` Adhemerval Zanella
2018-06-26 17:33   ` Florian Weimer [this message]
2018-06-26 17:52     ` Adhemerval Zanella
2018-09-18 10:51     ` Andreas Schwab
2018-09-18 10:59       ` Florian Weimer
2018-09-18 12:09         ` Andreas Schwab
2018-09-18 12:32           ` Florian Weimer
2018-06-26 18:16 ` DJ Delorie

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=df00e77d-528d-e410-ecb2-7699d45ebd5d@redhat.com \
    --to=fweimer@redhat.com \
    --cc=adhemerval.zanella@linaro.org \
    --cc=libc-alpha@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).