From: Andreas Schwab <schwab@redhat.com>
To: libc-hacker@sourceware.org
Subject: [PATCH] Fix error handling in getlogin_r
Date: Wed, 02 Jun 2010 11:45:00 -0000 [thread overview]
Message-ID: <m31vcpslsc.fsf@hase.home> (raw)
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."
next reply other threads:[~2010-06-02 11:45 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-06-02 11:45 Andreas Schwab [this message]
2010-06-15 0:53 ` Ulrich Drepper
2010-06-15 7:49 ` Andreas Schwab
2010-06-21 9:09 ` Andreas Schwab
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=m31vcpslsc.fsf@hase.home \
--to=schwab@redhat.com \
--cc=libc-hacker@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).