mirror of
https://github.com/python/cpython.git
synced 2025-07-07 19:35:27 +00:00
gh-127081: lock non-re-entrant *pwent
calls in free-threading (#132748)
This commit is contained in:
parent
2602d8ae98
commit
458e33018a
2 changed files with 22 additions and 5 deletions
|
@ -0,0 +1,2 @@
|
||||||
|
Fix libc thread safety issues with :mod:`pwd` by locking access to
|
||||||
|
``getpwall``.
|
|
@ -301,18 +301,33 @@ pwd_getpwall_impl(PyObject *module)
|
||||||
struct passwd *p;
|
struct passwd *p;
|
||||||
if ((d = PyList_New(0)) == NULL)
|
if ((d = PyList_New(0)) == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
|
#ifdef Py_GIL_DISABLED
|
||||||
|
static PyMutex getpwall_mutex = {0};
|
||||||
|
PyMutex_Lock(&getpwall_mutex);
|
||||||
|
#endif
|
||||||
|
int failure = 0;
|
||||||
|
PyObject *v = NULL;
|
||||||
setpwent();
|
setpwent();
|
||||||
while ((p = getpwent()) != NULL) {
|
while ((p = getpwent()) != NULL) {
|
||||||
PyObject *v = mkpwent(module, p);
|
v = mkpwent(module, p);
|
||||||
if (v == NULL || PyList_Append(d, v) != 0) {
|
if (v == NULL || PyList_Append(d, v) != 0) {
|
||||||
Py_XDECREF(v);
|
/* NOTE: cannot dec-ref here, while holding the mutex. */
|
||||||
Py_DECREF(d);
|
failure = 1;
|
||||||
endpwent();
|
goto done;
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
Py_DECREF(v);
|
Py_DECREF(v);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
done:
|
||||||
endpwent();
|
endpwent();
|
||||||
|
#ifdef Py_GIL_DISABLED
|
||||||
|
PyMutex_Unlock(&getpwall_mutex);
|
||||||
|
#endif
|
||||||
|
if (failure) {
|
||||||
|
Py_XDECREF(v);
|
||||||
|
Py_CLEAR(d);
|
||||||
|
}
|
||||||
return d;
|
return d;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue