Skip to content

gh-126316: Use critical sections in grp module #126488

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:mod:`grp`: Fix segfault on concurrent calls on Free Threaded build by using
critical sections. Patch by Victor Stinner.
16 changes: 14 additions & 2 deletions Modules/clinic/grpmodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 16 additions & 5 deletions Modules/grpmodule.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
/* UNIX group file access module */
/* UNIX group file access module
*
* Use @critical_section since getgrgid(), getgrnam() and getgrall() are not
* thread safe (gh-126316).
*/

// Need limited C API version 3.13 for PyMem_RawRealloc()
#include "pyconfig.h" // Py_GIL_DISABLED
#ifndef Py_GIL_DISABLED
#define Py_LIMITED_API 0x030d0000
# define Py_LIMITED_API 0x030d0000
#else
# ifndef Py_BUILD_CORE_BUILTIN
# define Py_BUILD_CORE_MODULE 1
# endif
#endif

#include "Python.h"
Expand Down Expand Up @@ -111,6 +119,7 @@ mkgrent(PyObject *module, struct group *p)
}

/*[clinic input]
@critical_section
grp.getgrgid

id: object
Expand All @@ -122,7 +131,7 @@ If id is not valid, raise KeyError.

static PyObject *
grp_getgrgid_impl(PyObject *module, PyObject *id)
/*[clinic end generated code: output=30797c289504a1ba input=15fa0e2ccf5cda25]*/
/*[clinic end generated code: output=30797c289504a1ba input=f5bf2ce5d1b3d6bc]*/
{
PyObject *retval = NULL;
int nomem = 0;
Expand Down Expand Up @@ -191,6 +200,7 @@ grp_getgrgid_impl(PyObject *module, PyObject *id)
}

/*[clinic input]
@critical_section
grp.getgrnam

name: unicode
Expand All @@ -202,7 +212,7 @@ If name is not valid, raise KeyError.

static PyObject *
grp_getgrnam_impl(PyObject *module, PyObject *name)
/*[clinic end generated code: output=67905086f403c21c input=08ded29affa3c863]*/
/*[clinic end generated code: output=67905086f403c21c input=da37ffb830c57159]*/
{
char *buf = NULL, *buf2 = NULL, *name_chars;
int nomem = 0;
Expand Down Expand Up @@ -269,6 +279,7 @@ grp_getgrnam_impl(PyObject *module, PyObject *name)
}

/*[clinic input]
@critical_section
grp.getgrall

Return a list of all available group entries, in arbitrary order.
Expand All @@ -279,7 +290,7 @@ to use YP/NIS and may not be accessible via getgrnam or getgrgid.

static PyObject *
grp_getgrall_impl(PyObject *module)
/*[clinic end generated code: output=585dad35e2e763d7 input=d7df76c825c367df]*/
/*[clinic end generated code: output=585dad35e2e763d7 input=b257403558f07a1d]*/
{
PyObject *d;
struct group *p;
Expand Down
Loading