1
0
Fork 0
mirror of https://gitlab.com/sortix/sortix.git synced 2023-02-13 20:55:38 -05:00

Fix getlogin_r memory leak.

This commit is contained in:
Jonas 'Sortie' Termansen 2015-02-04 17:07:13 +01:00
parent a22cca77f5
commit 8322d5f82c

View file

@ -38,6 +38,7 @@ extern "C" int getlogin_r(char* buf, size_t size)
size_t pwdbuflen = 0;
do
{
// TODO: Potential overflow.
size_t new_pwdbuflen = pwdbuflen ? 2 * pwdbuflen : 64;
char* new_pwdbuf = (char*) realloc(pwdbuf, new_pwdbuflen);
if ( !new_pwdbuf )
@ -50,10 +51,9 @@ extern "C" int getlogin_r(char* buf, size_t size)
return free(pwdbuf), errno = errnum, -1;
const char* username = passwd->pw_name;
size_t username_len = strlen(username);
if ( size < (username_len + 1) * sizeof(char) )
if ( size <= strlcpy(buf, username, size) )
return free(pwdbuf), errno = ERANGE, -1;
strcpy(buf, username);
free(pwdbuf);
return 0;
}