diff --git a/bson/_cbsonmodule.c b/bson/_cbsonmodule.c index 1eb324db4..390e92bb8 100644 --- a/bson/_cbsonmodule.c +++ b/bson/_cbsonmodule.c @@ -548,7 +548,6 @@ static int _write_element_to_buffer(PyObject* self, buffer_t buffer, int type_by if (!PyDict_Size(scope)) { Py_DECREF(scope); - *(buffer_get_buffer(buffer) + type_byte) = 0x0D; return write_string(buffer, value); } @@ -560,10 +559,12 @@ static int _write_element_to_buffer(PyObject* self, buffer_t buffer, int type_by length_location = buffer_save_space(buffer, 4); if (length_location == -1) { PyErr_NoMemory(); + Py_DECREF(scope); return 0; } if (!write_string(buffer, value)) { + Py_DECREF(scope); return 0; } @@ -758,12 +759,14 @@ static int _write_element_to_buffer(PyObject* self, buffer_t buffer, int type_by PyErr_SetString(InvalidStringData, "regex patterns must be valid UTF-8"); Py_DECREF(InvalidStringData); + Py_DECREF(encoded_pattern); return 0; } else if (status == HAS_NULL) { PyObject* InvalidDocument = _error("InvalidDocument"); PyErr_SetString(InvalidDocument, "regex patterns must not contain the NULL byte"); Py_DECREF(InvalidDocument); + Py_DECREF(encoded_pattern); return 0; } @@ -1221,6 +1224,7 @@ static PyObject* get_value(PyObject* self, const char* buffer, int* position, to_append = get_value(self, buffer, position, type, max - key_size, as_class, tz_aware, uuid_subtype); if (!to_append) { + Py_DECREF(value); return NULL; } PyList_Append(value, to_append); @@ -1421,6 +1425,7 @@ static PyObject* get_value(PyObject* self, const char* buffer, int* position, *position += pattern_length + 1; flags_length = strlen(buffer + *position); if (max < pattern_length + flags_length) { + Py_DECREF(pattern); goto invalid; } flags = 0; @@ -1461,6 +1466,7 @@ static PyObject* get_value(PyObject* self, const char* buffer, int* position, } *position += collection_length + 1; if (max < collection_length + 12) { + Py_DECREF(collection); goto invalid; } id = PyObject_CallFunction(state->ObjectId, "s#", buffer + *position, 12);