Skip to content

Commit c0d5c84

Browse files
committed
PYTHON-5571 - Fix memory leak when raising InvalidDocument with C extensions
1 parent e0767cf commit c0d5c84

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

bson/_cbsonmodule.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1657,26 +1657,28 @@ void handle_invalid_doc_error(PyObject* dict) {
16571657
}
16581658

16591659
if (evalue && PyErr_GivenExceptionMatches(etype, InvalidDocument)) {
1660-
PyObject *msg = PyObject_Str(evalue);
1660+
msg = PyObject_Str(evalue);
16611661
if (msg) {
16621662
const char * msg_utf8 = PyUnicode_AsUTF8(msg);
16631663
if (msg_utf8 == NULL) {
16641664
goto cleanup;
16651665
}
1666-
PyObject *new_msg = PyUnicode_FromFormat("Invalid document: %s", msg_utf8);
1666+
new_msg = PyUnicode_FromFormat("Invalid document: %s", msg_utf8);
16671667
if (new_msg == NULL) {
16681668
goto cleanup;
16691669
}
16701670
// Add doc to the error instance as a property.
1671-
PyObject *new_evalue = PyObject_CallFunctionObjArgs(InvalidDocument, new_msg, dict, NULL);
1671+
new_evalue = PyObject_CallFunctionObjArgs(InvalidDocument, new_msg, dict, NULL);
16721672
Py_DECREF(evalue);
16731673
Py_DECREF(etype);
16741674
etype = InvalidDocument;
16751675
InvalidDocument = NULL;
16761676
if (new_evalue) {
16771677
evalue = new_evalue;
1678+
new_evalue = NULL;
16781679
} else {
16791680
evalue = msg;
1681+
msg = NULL;
16801682
}
16811683
}
16821684
PyErr_NormalizeException(&etype, &evalue, &etrace);

0 commit comments

Comments
 (0)