Skip to content

gh-118605: Fix reference leak in FrameLocalsProxy #118607

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 3 commits into from
May 5, 2024
Merged
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
31 changes: 28 additions & 3 deletions Objects/frameobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,8 @@ framelocalsproxy_merge(PyObject* self, PyObject* other)
Py_DECREF(value);
}

Py_DECREF(iter);

return 0;
}

Expand All @@ -242,11 +244,18 @@ framelocalsproxy_keys(PyObject *self, PyObject *__unused)
PyFrameObject *frame = ((PyFrameLocalsProxyObject*)self)->frame;
PyCodeObject *co = _PyFrame_GetCode(frame->f_frame);

if (names == NULL) {
return NULL;
}

for (int i = 0; i < co->co_nlocalsplus; i++) {
PyObject *val = framelocalsproxy_getval(frame->f_frame, co, i);
if (val) {
PyObject *name = PyTuple_GET_ITEM(co->co_localsplusnames, i);
PyList_Append(names, name);
if (PyList_Append(names, name) < 0) {
Py_DECREF(names);
return NULL;
}
}
}

Expand All @@ -258,7 +267,10 @@ framelocalsproxy_keys(PyObject *self, PyObject *__unused)
if (frame->f_extra_locals) {
assert(PyDict_Check(frame->f_extra_locals));
while (PyDict_Next(frame->f_extra_locals, &i, &key, &value)) {
PyList_Append(names, key);
if (PyList_Append(names, key) < 0) {
Py_DECREF(names);
return NULL;
}
}
}

Expand Down Expand Up @@ -306,7 +318,15 @@ framelocalsproxy_visit(PyObject *self, visitproc visit, void *arg)
static PyObject *
framelocalsproxy_iter(PyObject *self)
{
return PyObject_GetIter(framelocalsproxy_keys(self, NULL));
PyObject* keys = framelocalsproxy_keys(self, NULL);
Copy link
Member

Choose a reason for hiding this comment

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

Probably should error-check this too, and the PyList_Append in that function could fail so should clean up and return NULL.

Copy link
Member

Choose a reason for hiding this comment

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

Also, there is a lack of error checking in some new "frameobject.c" functions. I will create a separate issue for this.

Copy link
Member Author

Choose a reason for hiding this comment

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

I fixed the framelocalsproxy_keys related checks. We can address the others in a seperate PR?

if (keys == NULL) {
return NULL;
}

PyObject* iter = PyObject_GetIter(keys);
Py_XDECREF(keys);

return iter;
}

static PyObject *
Expand Down Expand Up @@ -567,6 +587,11 @@ static PyObject*
framelocalsproxy_reversed(PyObject *self, PyObject *__unused)
{
PyObject *result = framelocalsproxy_keys(self, NULL);

if (result == NULL) {
return NULL;
}

if (PyList_Reverse(result) < 0) {
Py_DECREF(result);
return NULL;
Expand Down
Loading