Skip to content
Closed
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fix up reference counting in Python/pystate.c:stdio_at_fork_reinit
  • Loading branch information
notarealdeveloper committed Dec 31, 2021
commit f5334b40cea86a978715c7f13f7d31de97a7d1dc
27 changes: 10 additions & 17 deletions Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -179,27 +179,18 @@ static int stdio_at_fork_reinit(_Py_Identifier *key)

int ret = 0;

PyObject *stdio, *closed, *isatty, *buffer, *result;
PyObject *isatty = NULL;
PyObject *buffer = NULL;
PyObject *result = NULL;

stdio = _PySys_GetObjectId(key);

_Py_IDENTIFIER(closed);
if (_PyObject_LookupAttrId(stdio, &PyId_closed, &closed) < 0) {
PyErr_Clear();
goto end;
}

if (Py_IsTrue(closed)) {
goto end;
}
PyObject *stdio = _PySys_GetObjectId(key);

_Py_IDENTIFIER(isatty);
if (_PyObject_GetAttrId(stdio, &PyId_isatty) == NULL) {
isatty = _PyObject_CallMethodIdNoArgs(stdio, &PyId_isatty);
if (isatty == NULL) {
PyErr_Clear();
goto end;
}

isatty = _PyObject_CallMethodIdNoArgs(stdio, &PyId_isatty);
if (Py_IsFalse(isatty)) {
goto end;
}
Expand All @@ -219,14 +210,16 @@ static int stdio_at_fork_reinit(_Py_Identifier *key)
}

result = _PyObject_CallMethodIdNoArgs(buffer, &PyId__at_fork_reinit);

if (result == Py_True) {
if (Py_IsTrue(result)) {
goto end;
}

/* error */
ret = -1;
end:
Py_XDECREF(isatty);
Py_XDECREF(buffer);
Py_XDECREF(result);
return ret;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there are a few more leaks here. Perhaps it would be simpler to initialize the variables to NULL, and then use Py_XDECREF in the end:/error: label.

}

Expand Down