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>
* dlfcn/Makefile: Remove explicit dependencies on libc.so and

View File

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

View File

@ -58,30 +58,31 @@ __getlogin_r_loginuid (name, namesize)
bool use_malloc = false;
struct passwd pwd;
struct passwd *tpwd;
int result = 0;
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))
extend_alloca (buf, buflen, 2 * buflen);
buf = 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;
result = ENOMEM;
goto out;
}
buf = newp;
use_malloc = true;
}
if (tpwd == NULL)
goto fail;
if (res != 0)
{
result = -1;
goto out;
}
int result = 0;
size_t needed = strlen (pwd.pw_name) + 1;
if (needed > namesize)
{
@ -109,8 +110,9 @@ getlogin_r (name, namesize)
char *name;
size_t namesize;
{
if (__getlogin_r_loginuid (name, namesize) == 0)
return 0;
int res = __getlogin_r_loginuid (name, namesize);
if (res >= 0)
return res;
return getlogin_r_fd0 (name, namesize);
}