[v2,09/12] getlogin_r: switch Linux variant to struct scratch_buffer

Message ID 1517837254-19399-10-git-send-email-adhemerval.zanella@linaro.org
State New
Headers show
Series
  • posix: glob/fnmatch fixes and refactor
Related show

Commit Message

Adhemerval Zanella Feb. 5, 2018, 1:27 p.m.
From: Florian Weimer <fweimer@redhat.com>

	[BZ #18023]
	* sysdeps/unix/sysv/linux/getlogin_r.c (__getlogin_r_loginuid):
	Use scratch_buffer instead of extend_alloca.
---
 ChangeLog                            |  6 ++++++
 sysdeps/unix/sysv/linux/getlogin_r.c | 34 +++++++++++++---------------------
 2 files changed, 19 insertions(+), 21 deletions(-)

Comments

Florian Weimer Feb. 5, 2018, 2:05 p.m. | #1
On 02/05/2018 02:27 PM, Adhemerval Zanella wrote:
> From: Florian Weimer <fweimer@redhat.com>
> 
> 	[BZ #18023]
> 	* sysdeps/unix/sysv/linux/getlogin_r.c (__getlogin_r_loginuid):
> 	Use scratch_buffer instead of extend_alloca.

Still looks okay to me.  Do you want me to commit it?

Thanks,
Florian
Adhemerval Zanella Feb. 5, 2018, 3:06 p.m. | #2
On 05/02/2018 12:05, Florian Weimer wrote:
> On 02/05/2018 02:27 PM, Adhemerval Zanella wrote:
>> From: Florian Weimer <fweimer@redhat.com>
>>
>>     [BZ #18023]
>>     * sysdeps/unix/sysv/linux/getlogin_r.c (__getlogin_r_loginuid):
>>     Use scratch_buffer instead of extend_alloca.
> 
> Still looks okay to me.  Do you want me to commit it?
> 
> Thanks,
> Florian

I can do it for you.
Florian Weimer Feb. 5, 2018, 3:15 p.m. | #3
On 02/05/2018 04:06 PM, Adhemerval Zanella wrote:
> 
> 
> On 05/02/2018 12:05, Florian Weimer wrote:
>> On 02/05/2018 02:27 PM, Adhemerval Zanella wrote:
>>> From: Florian Weimer <fweimer@redhat.com>
>>>
>>>      [BZ #18023]
>>>      * sysdeps/unix/sysv/linux/getlogin_r.c (__getlogin_r_loginuid):
>>>      Use scratch_buffer instead of extend_alloca.
>>
>> Still looks okay to me.  Do you want me to commit it?
>>
>> Thanks,
>> Florian
> 
> I can do it for you.

Okay, please do.

Thanks,
Florian

Patch

diff --git a/sysdeps/unix/sysv/linux/getlogin_r.c b/sysdeps/unix/sysv/linux/getlogin_r.c
index 84c51d0..73ea14c 100644
--- a/sysdeps/unix/sysv/linux/getlogin_r.c
+++ b/sysdeps/unix/sysv/linux/getlogin_r.c
@@ -18,6 +18,7 @@ 
 #include <pwd.h>
 #include <unistd.h>
 #include <not-cancel.h>
+#include <scratch_buffer.h>
 
 #define STATIC static
 static int getlogin_r_fd0 (char *name, size_t namesize);
@@ -54,29 +55,22 @@  __getlogin_r_loginuid (char *name, size_t namesize)
 	  endp == uidbuf || *endp != '\0'))
     return -1;
 
-  size_t buflen = 1024;
-  char *buf = alloca (buflen);
-  bool use_malloc = false;
   struct passwd pwd;
   struct passwd *tpwd;
   int result = 0;
   int res;
+  struct scratch_buffer tmpbuf;
+  scratch_buffer_init (&tmpbuf);
 
-  while ((res = __getpwuid_r (uid, &pwd, buf, buflen, &tpwd)) == ERANGE)
-    if (__libc_use_alloca (2 * buflen))
-      buf = extend_alloca (buf, buflen, 2 * buflen);
-    else
-      {
-	buflen *= 2;
-	char *newp = realloc (use_malloc ? buf : NULL, buflen);
-	if (newp == NULL)
-	  {
-	    result = ENOMEM;
-	    goto out;
-	  }
-	buf = newp;
-	use_malloc = true;
-      }
+  while ((res =  __getpwuid_r (uid, &pwd,
+			       tmpbuf.data, tmpbuf.length, &tpwd)) == ERANGE)
+    {
+      if (!scratch_buffer_grow (&tmpbuf))
+	{
+	  result = ENOMEM;
+	  goto out;
+	}
+    }
 
   if (res != 0 || tpwd == NULL)
     {
@@ -95,9 +89,7 @@  __getlogin_r_loginuid (char *name, size_t namesize)
   memcpy (name, pwd.pw_name, needed);
 
  out:
-  if (use_malloc)
-    free (buf);
-
+  scratch_buffer_free (&tmpbuf);
   return result;
 }