Skip to content

Commit a577545

Browse files
author
A. Jesse Jiryu Davis
committed
Fix refleak in insert. PYTHON-564
1 parent a83f1d6 commit a577545

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

pymongo/_cmessagemodule.c

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,9 @@ static PyObject* _cbson_do_batched_insert(PyObject* self, PyObject* args) {
555555
PyObject* client;
556556
PyObject* last_error_args;
557557
PyObject* result;
558+
PyObject* max_bson_size_obj;
559+
PyObject* max_message_size_obj;
560+
PyObject* send_message_result;
558561
unsigned char check_keys;
559562
unsigned char safe;
560563
unsigned char continue_on_error;
@@ -578,24 +581,25 @@ static PyObject* _cbson_do_batched_insert(PyObject* self, PyObject* args) {
578581
options += 1;
579582
}
580583

584+
max_bson_size_obj = PyObject_GetAttrString(client, "max_bson_size");
581585
#if PY_MAJOR_VERSION >= 3
582-
max_bson_size = PyLong_AsLong(
583-
PyObject_GetAttrString(client, "max_bson_size"));
586+
max_bson_size = PyLong_AsLong(max_bson_size_obj);
584587
#else
585-
max_bson_size = PyInt_AsLong(
586-
PyObject_GetAttrString(client, "max_bson_size"));
588+
max_bson_size = PyInt_AsLong(max_bson_size_obj);
587589
#endif
590+
Py_XDECREF(max_bson_size_obj);
588591
if (max_bson_size == -1) {
589592
PyMem_Free(collection_name);
590593
return NULL;
591594
}
595+
596+
max_message_size_obj = PyObject_GetAttrString(client, "max_message_size");
592597
#if PY_MAJOR_VERSION >= 3
593-
max_message_size = PyLong_AsLong(
594-
PyObject_GetAttrString(client, "max_message_size"));
598+
max_message_size = PyLong_AsLong(max_message_size_obj);
595599
#else
596-
max_message_size = PyInt_AsLong(
597-
PyObject_GetAttrString(client, "max_message_size"));
600+
max_message_size = PyInt_AsLong(max_message_size_obj);
598601
#endif
602+
Py_XDECREF(max_message_size_obj);
599603
if (max_message_size == -1) {
600604
PyMem_Free(collection_name);
601605
return NULL;
@@ -707,8 +711,10 @@ static PyObject* _cbson_do_batched_insert(PyObject* self, PyObject* args) {
707711
request_id = new_request_id;
708712
length_location = message_start;
709713

710-
if (!PyObject_CallMethod(client,
711-
"_send_message", "NO", result, send_gle)) {
714+
send_message_result = PyObject_CallMethod(client, "_send_message",
715+
"NO", result, send_gle);
716+
717+
if (!send_message_result) {
712718
PyObject *etype = NULL, *evalue = NULL, *etrace = NULL;
713719
PyObject* OperationFailure;
714720
PyErr_Fetch(&etype, &evalue, &etrace);
@@ -746,6 +752,8 @@ static PyObject* _cbson_do_batched_insert(PyObject* self, PyObject* args) {
746752
* acknowledged writes. Re-raise immediately. */
747753
PyErr_Restore(etype, evalue, etrace);
748754
goto iterfail;
755+
} else {
756+
Py_DECREF(send_message_result);
749757
}
750758
}
751759
}
@@ -783,12 +791,17 @@ static PyObject* _cbson_do_batched_insert(PyObject* self, PyObject* args) {
783791
buffer_free(buffer);
784792

785793
/* Send the last (or only) batch */
786-
if (!PyObject_CallMethod(client, "_send_message", "NN",
787-
result, PyBool_FromLong((long)safe))) {
794+
send_message_result = PyObject_CallMethod(client, "_send_message", "NN",
795+
result,
796+
PyBool_FromLong((long)safe));
797+
798+
if (!send_message_result) {
788799
Py_XDECREF(exc_type);
789800
Py_XDECREF(exc_value);
790801
Py_XDECREF(exc_trace);
791802
return NULL;
803+
} else {
804+
Py_DECREF(send_message_result);
792805
}
793806

794807
if (exc_type) {

0 commit comments

Comments
 (0)