Fix error handling in Linux getlogin*.

This commit is contained in:
Ulrich Drepper 2010-06-19 09:54:28 -07:00
parent 765ade4b29
commit 63c4ed22b5
3 changed files with 25 additions and 13 deletions

View File

@ -1,3 +1,12 @@
2010-06-19 Ulrich Drepper <drepper@redhat.com>
* sysdeps/unix/sysv/linux/getlogin_r.c (__getlogin_r_loginuid): Handle
OOM in getpwuid_r correctly. Return error number when the caller
should return, otherwise -1.
(getlogin_r): Adjust to return also for result of __getlogin_r_loginuid
call returning > 0 value.
* sysdeps/unix/sysv/linux/getlogin.c (getlogin): Likewise.
2010-06-07 Andreas Schwab <schwab@redhat.com> 2010-06-07 Andreas Schwab <schwab@redhat.com>
* dlfcn/Makefile: Remove explicit dependencies on libc.so and * dlfcn/Makefile: Remove explicit dependencies on libc.so and

View File

@ -32,8 +32,9 @@
char * char *
getlogin (void) getlogin (void)
{ {
if (__getlogin_r_loginuid (name, sizeof (name)) == 0) int res = __getlogin_r_loginuid (name, sizeof (name));
return name; if (res >= 0)
return res == 0 ? name : NULL;
return getlogin_fd0 (); return getlogin_fd0 ();
} }

View File

@ -58,30 +58,31 @@ __getlogin_r_loginuid (name, namesize)
bool use_malloc = false; bool use_malloc = false;
struct passwd pwd; struct passwd pwd;
struct passwd *tpwd; struct passwd *tpwd;
int result = 0;
int res; int res;
while ((res = __getpwuid_r (uid, &pwd, buf, buflen, &tpwd)) != 0) while ((res = __getpwuid_r (uid, &pwd, buf, buflen, &tpwd)) == ERANGE)
if (__libc_use_alloca (2 * buflen)) if (__libc_use_alloca (2 * buflen))
extend_alloca (buf, buflen, 2 * buflen); buf = extend_alloca (buf, buflen, 2 * buflen);
else else
{ {
buflen *= 2; buflen *= 2;
char *newp = realloc (use_malloc ? buf : NULL, buflen); char *newp = realloc (use_malloc ? buf : NULL, buflen);
if (newp == NULL) if (newp == NULL)
{ {
fail: result = ENOMEM;
if (use_malloc) goto out;
free (buf);
return 1;
} }
buf = newp; buf = newp;
use_malloc = true; use_malloc = true;
} }
if (tpwd == NULL) if (res != 0)
goto fail; {
result = -1;
goto out;
}
int result = 0;
size_t needed = strlen (pwd.pw_name) + 1; size_t needed = strlen (pwd.pw_name) + 1;
if (needed > namesize) if (needed > namesize)
{ {
@ -109,8 +110,9 @@ getlogin_r (name, namesize)
char *name; char *name;
size_t namesize; size_t namesize;
{ {
if (__getlogin_r_loginuid (name, namesize) == 0) int res = __getlogin_r_loginuid (name, namesize);
return 0; if (res >= 0)
return res;
return getlogin_r_fd0 (name, namesize); return getlogin_r_fd0 (name, namesize);
} }