Skip to content

Commit d47b85c

Browse files
committed
pythongh-116738: Assert list is uniquely referenced
1 parent 70ba3ab commit d47b85c

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

Modules/grpmodule.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "Python.h"
99
#include "posixmodule.h"
1010
#include "pycore_list.h" // _PyList_AppendTakeRef()
11+
#include "pycore_object.h" // _PyObject_IsUniquelyReferenced()
1112

1213
#include <errno.h> // ERANGE
1314
#include <grp.h> // getgrgid_r()
@@ -291,13 +292,14 @@ grp_getgrall_impl(PyObject *module)
291292
setgrent();
292293

293294
struct group *p;
295+
// `d` is a local list; append items without a lock using
296+
// _PyList_AppendTakeRef() within the loop.
297+
assert(_PyObject_IsUniquelyReferenced(d));
294298
while ((p = getgrent()) != NULL) {
295299
// gh-126316: Don't release the mutex around mkgrent() since
296300
// setgrent()/endgrent() are not reentrant / thread-safe. A deadlock
297301
// is unlikely since mkgrent() should not be able to call arbitrary
298302
// Python code.
299-
// `d` is a local list; append to it without a lock using
300-
// _PyList_AppendTakeRef().
301303
PyObject *v = mkgrent(module, p);
302304
if (v == NULL
303305
|| _PyList_AppendTakeRef((PyListObject *)d, Py_NewRef(v)) != 0)

0 commit comments

Comments
 (0)