Skip to content
Closed
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix a memory leak in the :mod:`json` module when encoding objects with a
Comment thread
okiemute04 marked this conversation as resolved.
Outdated
custom ``default()`` function that raises an exception, when a recursion
error occurs, or when nested encoding fails.
7 changes: 6 additions & 1 deletion Modules/_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -1632,8 +1632,12 @@ encoder_listencode_obj(PyEncoderObject *s, PyUnicodeWriter *writer,
}

if (_Py_EnterRecursiveCall(" while encoding a JSON object")) {
if (ident != NULL) {
PyDict_DelItem(s->markers, ident);
Comment thread
okiemute04 marked this conversation as resolved.
Outdated
Py_XDECREF(ident);
}
Comment on lines +1635 to +1637
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.

Suggested change
if (ident != NULL) {
int del_rv = PyDict_DelItem(s->markers, ident);
Py_DECREF(ident);
if (del_rv < 0) {
Py_DECREF(newobj);
return -1;
}
}
if (ident != NULL) {
(void)PyDict_DelItem(s->markers, ident);
}

Actually since we are anyway falling through the return -1 case we can simplify this as follows. Then keep the XDECREF for indent. However explicitly suppress the error code of DelItem.

Py_DECREF(newobj);
Py_XDECREF(ident);

Comment thread
okiemute04 marked this conversation as resolved.
Outdated
return -1;
}
rv = encoder_listencode_obj(s, writer, newobj, indent_level, indent_cache);
Expand All @@ -1642,6 +1646,7 @@ encoder_listencode_obj(PyEncoderObject *s, PyUnicodeWriter *writer,
Py_DECREF(newobj);
if (rv) {
_PyErr_FormatNote("when serializing %T object", obj);
Comment thread
okiemute04 marked this conversation as resolved.

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.

Please revert this

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

okay

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.

Revert cosmetic changes. Nothing was properly reverted.

Py_XDECREF(ident);
return -1;
}
Expand Down
Loading