Skip to content

Commit 78c9cde

Browse files
committed
pythongh-127081: lock non-re-entrant *pwent calls
The libc setpwent, getpwent, and endpwent functions are not thread-safe. Protect them with mutexs in free-threading builds.
1 parent ccad61e commit 78c9cde

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix libc thread safety issues with :mod:`pwd` by locking access to
2+
``getpwall``.

Modules/pwdmodule.c

+13-3
Original file line numberDiff line numberDiff line change
@@ -301,18 +301,28 @@ pwd_getpwall_impl(PyObject *module)
301301
struct passwd *p;
302302
if ((d = PyList_New(0)) == NULL)
303303
return NULL;
304+
305+
#ifdef Py_GIL_DISABLED
306+
static PyMutex getpwall_mutex = {0};
307+
PyMutex_Lock(&getpwall_mutex);
308+
#endif
304309
setpwent();
310+
305311
while ((p = getpwent()) != NULL) {
306312
PyObject *v = mkpwent(module, p);
307313
if (v == NULL || PyList_Append(d, v) != 0) {
308314
Py_XDECREF(v);
309-
Py_DECREF(d);
310-
endpwent();
311-
return NULL;
315+
Py_CLEAR(d);
316+
goto done;
312317
}
313318
Py_DECREF(v);
314319
}
320+
321+
done:
315322
endpwent();
323+
#ifdef Py_GIL_DISABLED
324+
PyMutex_Unlock(&getpwall_mutex);
325+
#endif
316326
return d;
317327
}
318328
#endif

0 commit comments

Comments
 (0)