Skip to content

Commit b74abf2

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

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,10 +1657,10 @@ 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
// Prepend doc to the existing message
1663-
PyObject *dict_str = PyObject_Str(dict);
1663+
dict_str = PyObject_Str(dict);
16641664
if (dict_str == NULL) {
16651665
goto cleanup;
16661666
}
@@ -1672,15 +1672,17 @@ void handle_invalid_doc_error(PyObject* dict) {
16721672
if (msg_utf8 == NULL) {
16731673
goto cleanup;
16741674
}
1675-
PyObject *new_msg = PyUnicode_FromFormat("Invalid document %s | %s", dict_str_utf8, msg_utf8);
1675+
new_msg = PyUnicode_FromFormat("Invalid document %s | %s", dict_str_utf8, msg_utf8);
16761676
Py_DECREF(evalue);
16771677
Py_DECREF(etype);
16781678
etype = InvalidDocument;
16791679
InvalidDocument = NULL;
16801680
if (new_msg) {
16811681
evalue = new_msg;
1682+
new_msg = NULL;
16821683
} else {
16831684
evalue = msg;
1685+
msg = NULL;
16841686
}
16851687
}
16861688
PyErr_NormalizeException(&etype, &evalue, &etrace);

0 commit comments

Comments
 (0)