Content-Length: 338626 | pFad | http://github.com/python/cpython/pull/135434/commits/63360a0581fa0b7cceed46a0542b34722880bd1c

04 gh-116738: Make grp module thread-safe by yoney · Pull Request #135434 · python/cpython · GitHub
Skip to content

gh-116738: Make grp module thread-safe #135434

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jul 14, 2025
Prev Previous commit
Next Next commit
gh-116738: Protect non thread-safe functions in default build
  • Loading branch information
yoney committed Jun 26, 2025
commit 63360a0581fa0b7cceed46a0542b34722880bd1c
30 changes: 11 additions & 19 deletions Modules/grpmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ grp_getgrgid_impl(PyObject *module, PyObject *id)
if (!_Py_Gid_Converter(id, &gid)) {
return NULL;
}
#if defined(HAVE_GETGRGID_R)
#ifdef HAVE_GETGRGID_R
int status;
Py_ssize_t bufsize;
/* Note: 'grp' will be used via pointer 'p' on getgrgid_r success. */
Expand Down Expand Up @@ -167,21 +167,17 @@ grp_getgrgid_impl(PyObject *module, PyObject *id)
}

Py_END_ALLOW_THREADS
#elif defined(Py_GIL_DISABLED)
#else
static PyMutex getgrgid_mutex = {0};
PyMutex_Lock(&getgrgid_mutex);
// The getgrgid() function need not be thread-safe.
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/getgrgid.html
p = getgrgid(gid);
#endif
if (p == NULL) {
// Unlock the mutex on error. The following error handling block will
// handle the rest.
#ifndef HAVE_GETGRGID_R
PyMutex_Unlock(&getgrgid_mutex);
}
#else
p = getgrgid(gid);
#endif
if (p == NULL) {
PyMem_RawFree(buf);
if (nomem == 1) {
return PyErr_NoMemory();
Expand All @@ -194,9 +190,9 @@ grp_getgrgid_impl(PyObject *module, PyObject *id)
return NULL;
}
retval = mkgrent(module, p);
#if defined(HAVE_GETGRGID_R)
#ifdef HAVE_GETGRGID_R
PyMem_RawFree(buf);
#elif defined(Py_GIL_DISABLED)
#else
PyMutex_Unlock(&getgrgid_mutex);
#endif
return retval;
Expand Down Expand Up @@ -226,7 +222,7 @@ grp_getgrnam_impl(PyObject *module, PyObject *name)
/* check for embedded null bytes */
if (PyBytes_AsStringAndSize(bytes, &name_chars, NULL) == -1)
goto out;
#if defined(HAVE_GETGRNAM_R)
#ifdef HAVE_GETGRNAM_R
int status;
Py_ssize_t bufsize;
/* Note: 'grp' will be used via pointer 'p' on getgrnam_r success. */
Expand Down Expand Up @@ -261,21 +257,17 @@ grp_getgrnam_impl(PyObject *module, PyObject *name)
}

Py_END_ALLOW_THREADS
#elif defined(Py_GIL_DISABLED)
#else
static PyMutex getgrnam_mutex = {0};
PyMutex_Lock(&getgrnam_mutex);
// The getgrnam() function need not be thread-safe.
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/getgrnam.html
p = getgrnam(name_chars);
#endif
if (p == NULL) {
// Unlock the mutex on error. The following error handling block will
// handle the rest.
#ifndef HAVE_GETGRGID_R
PyMutex_Unlock(&getgrnam_mutex);
}
#else
p = getgrnam(name_chars);
#endif
if (p == NULL) {
if (nomem == 1) {
PyErr_NoMemory();
}
Expand All @@ -285,7 +277,7 @@ grp_getgrnam_impl(PyObject *module, PyObject *name)
goto out;
}
retval = mkgrent(module, p);
#if !defined(HAVE_GETGRNAM_R) && defined(Py_GIL_DISABLED)
#ifndef HAVE_GETGRNAM_R
PyMutex_Unlock(&getgrnam_mutex);
#endif
out:
Expand Down








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/python/cpython/pull/135434/commits/63360a0581fa0b7cceed46a0542b34722880bd1c

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy