[3.14] gh-127081: use getlogin_r if available (gh-132751) (gh-135097)

The `getlogin` function is not thread-safe: replace with `getlogin_r` where
available.
(cherry picked from commit 1ffe913c20)

Co-authored-by: Duane Griffin <duaneg@dghda.com>
This commit is contained in:
Miss Islington (bot) 2025-06-03 20:41:47 +02:00 committed by GitHub
parent 81999f157f
commit d8c2bfac1f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 79 additions and 1 deletions

View file

@ -9548,6 +9548,24 @@ os_getlogin_impl(PyObject *module)
}
else
result = PyErr_SetFromWindowsErr(GetLastError());
#elif defined (HAVE_GETLOGIN_R)
# if defined (HAVE_MAXLOGNAME)
char name[MAXLOGNAME + 1];
# elif defined (HAVE_UT_NAMESIZE)
char name[UT_NAMESIZE + 1];
# else
char name[256];
# endif
int err = getlogin_r(name, sizeof(name));
if (err) {
int old_errno = errno;
errno = -err;
posix_error();
errno = old_errno;
}
else {
result = PyUnicode_DecodeFSDefault(name);
}
#else
char *name;
int old_errno = errno;