Skip to content

Commit 7ca6858

Browse files
committed
Once the newly created pwent items are successfully added to the list they are
strongly referenced by that, so we can dec-ref them safely.
1 parent 33f7fea commit 7ca6858

1 file changed

Lines changed: 5 additions & 11 deletions

File tree

Modules/pwdmodule.c

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -308,31 +308,25 @@ pwd_getpwall_impl(PyObject *module)
308308
PyMutex_Lock(&getpwall_mutex);
309309
#endif
310310
int failure = 0;
311-
PyObject *orphan = NULL;
311+
PyObject *v = NULL;
312312
setpwent();
313313
while ((p = getpwent()) != NULL) {
314-
/* NOTE: Ref counts are not decremented here, as we cannot allow
315-
* re-entrancy while holding the mutex. */
316-
PyObject *v = mkpwent(module, p);
314+
v = mkpwent(module, p);
317315
if (v == NULL || PyList_Append(d, v) != 0) {
318-
orphan = v;
316+
/* NOTE: cannot dec-ref here, while holding the mutex. */
319317
failure = 1;
320318
goto done;
321319
}
320+
Py_DECREF(v);
322321
}
323322

324323
done:
325324
endpwent();
326325
#ifdef Py_GIL_DISABLED
327326
PyMutex_Unlock(&getpwall_mutex);
328327
#endif
329-
/* Deferred decref on entries created above and added to the list. */
330-
Py_ssize_t n = PyList_Size(d);
331-
while (--n >= 0) {
332-
Py_DECREF(PyList_GetItem(d, n));
333-
}
334328
if (failure) {
335-
Py_XDECREF(orphan);
329+
Py_XDECREF(v);
336330
Py_CLEAR(d);
337331
}
338332
return d;

0 commit comments

Comments
 (0)