Skip to content

Commit 434c1a0

Browse files
committed
gh-127081: use getlogin_r if available
The getlogin function is not thread-safe: replace with getlogin_r where available. Note that this function is untested (unit test is skipped with a note it caused CI failures as behaviour differs between NIX environments).
1 parent ccad61e commit 434c1a0

File tree

5 files changed

+24
-1
lines changed

5 files changed

+24
-1
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix libc thread safety issues with :mod:`os` by replacing ``getlogin`` with
2+
``getlogin_r`` re-entrant version.

Modules/posixmodule.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9538,6 +9538,18 @@ os_getlogin_impl(PyObject *module)
95389538
}
95399539
else
95409540
result = PyErr_SetFromWindowsErr(GetLastError());
9541+
#elif defined (HAVE_GETLOGIN_R)
9542+
/* AFAIK the maximum length should be 32, but this is not checkable */
9543+
char name[64];
9544+
int err = getlogin_r(name, sizeof(name));
9545+
if (err) {
9546+
int old_errno = errno;
9547+
errno = -err;
9548+
posix_error();
9549+
errno = old_errno;
9550+
} else {
9551+
result = PyUnicode_DecodeFSDefault(name);
9552+
}
95419553
#else
95429554
char *name;
95439555
int old_errno = errno;

configure

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

configure.ac

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5137,7 +5137,7 @@ AC_CHECK_FUNCS([ \
51375137
faccessat fchmod fchmodat fchown fchownat fdopendir fdwalk fexecve \
51385138
fork fork1 fpathconf fstatat ftime ftruncate futimens futimes futimesat \
51395139
gai_strerror getegid geteuid getgid getgrent getgrgid getgrgid_r \
5140-
getgrnam_r getgrouplist gethostname getitimer getloadavg getlogin \
5140+
getgrnam_r getgrouplist gethostname getitimer getloadavg getlogin getlogin_r \
51415141
getpeername getpgid getpid getppid getpriority _getpty \
51425142
getpwent getpwnam_r getpwuid getpwuid_r getresgid getresuid getrusage getsid getspent \
51435143
getspnam getuid getwd grantpt if_nameindex initgroups kill killpg lchown linkat \

pyconfig.h.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,9 @@
530530
/* Define to 1 if you have the 'getlogin' function. */
531531
#undef HAVE_GETLOGIN
532532

533+
/* Define to 1 if you have the 'getlogin_r' function. */
534+
#undef HAVE_GETLOGIN_R
535+
533536
/* Define to 1 if you have the 'getnameinfo' function. */
534537
#undef HAVE_GETNAMEINFO
535538

0 commit comments

Comments
 (0)