public inbox for libc-hacker@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Fix error handling in getlogin_r
@ 2010-06-02 11:45 Andreas Schwab
  2010-06-15  0:53 ` Ulrich Drepper
  0 siblings, 1 reply; 4+ messages in thread
From: Andreas Schwab @ 2010-06-02 11:45 UTC (permalink / raw)
  To: libc-hacker

2010-06-02  Andreas Schwab  <schwab@redhat.com>

	* sysdeps/unix/sysv/linux/getlogin_r.c (__getlogin_r_loginuid):
	Fix error handling.  Properly resize buffer.  Don't set errno.
	(getlogin_r): Only use fallback if __getlogin_r_loginuid returns -1.
---
 sysdeps/unix/sysv/linux/getlogin_r.c |   63 ++++++++++++++++++++--------------
 1 files changed, 37 insertions(+), 26 deletions(-)

diff --git a/sysdeps/unix/sysv/linux/getlogin_r.c b/sysdeps/unix/sysv/linux/getlogin_r.c
index dad2671..404c84d 100644
--- a/sysdeps/unix/sysv/linux/getlogin_r.c
+++ b/sysdeps/unix/sysv/linux/getlogin_r.c
@@ -27,6 +27,10 @@ static int getlogin_r_fd0 (char *name, size_t namesize);
 #undef getlogin_r
 
 
+/* Try to determine login name from /proc/self/loginuid and return 0
+   if successful.  If /proc/self/loginuid cannot be read return -1.
+   Otherwise return the error number.  */
+
 int
 attribute_hidden
 __getlogin_r_loginuid (name, namesize)
@@ -35,7 +39,7 @@ __getlogin_r_loginuid (name, namesize)
 {
   int fd = open_not_cancel_2 ("/proc/self/loginuid", O_RDONLY);
   if (fd == -1)
-    return 1;
+    return -1;
 
   /* We are reading a 32-bit number.  12 bytes are enough for the text
      representation.  If not, something is wrong.  */
@@ -51,46 +55,50 @@ __getlogin_r_loginuid (name, namesize)
       || (uidbuf[n] = '\0',
 	  uid = strtoul (uidbuf, &endp, 10),
 	  endp == uidbuf || *endp != '\0'))
-    return 1;
+    return -1;
 
   size_t buflen = 1024;
   char *buf = alloca (buflen);
   bool use_malloc = false;
   struct passwd pwd;
   struct passwd *tpwd;
-  int res;
-
-  while ((res = __getpwuid_r (uid, &pwd, buf, buflen, &tpwd)) != 0)
-    if (__libc_use_alloca (2 * buflen))
-      extend_alloca (buf, buflen, 2 * buflen);
-    else
-      {
-	buflen *= 2;
-	char *newp = realloc (use_malloc ? buf : NULL, buflen);
-	if (newp == NULL)
-	  {
-	  fail:
-	    if (use_malloc)
-	      free (buf);
-	    return 1;
-	  }
-	buf = newp;
-	use_malloc = true;
-      }
+  int result;
+
+  while ((result = __getpwuid_r (uid, &pwd, buf, buflen, &tpwd)) != 0)
+    {
+      if (result != ERANGE)
+	goto out;
+      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;
+	}
+    }
 
   if (tpwd == NULL)
-    goto fail;
+    {
+      result = ENOENT;
+      goto out;
+    }
 
-  int result = 0;
   size_t needed = strlen (pwd.pw_name) + 1;
   if (needed > namesize)
     {
-      __set_errno (ERANGE);
       result = ERANGE;
       goto out;
     }
 
   memcpy (name, pwd.pw_name, needed);
+  result = 0;
 
  out:
   if (use_malloc)
@@ -109,8 +117,11 @@ getlogin_r (name, namesize)
      char *name;
      size_t namesize;
 {
-  if (__getlogin_r_loginuid (name, namesize) == 0)
-    return 0;
+  int result;
+
+  result = __getlogin_r_loginuid (name, namesize);
+  if (result >= 0)
+    return result;
 
   return getlogin_r_fd0 (name, namesize);
 }
-- 
1.7.1


-- 
Andreas Schwab, schwab@redhat.com
GPG Key fingerprint = D4E8 DBE3 3813 BB5D FA84  5EC7 45C6 250E 6F00 984E
"And now for something completely different."

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2010-06-21  9:09 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-06-02 11:45 [PATCH] Fix error handling in getlogin_r Andreas Schwab
2010-06-15  0:53 ` Ulrich Drepper
2010-06-15  7:49   ` Andreas Schwab
2010-06-21  9:09   ` Andreas Schwab

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).