Skip to content

Commit dc2cc10

Browse files
committed
PYTHON-5013 Add NULL checks in InvalidDocument bson handling
1 parent fd5a105 commit dc2cc10

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

bson/_cbsonmodule.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1747,6 +1747,9 @@ int write_dict(PyObject* self, buffer_t buffer,
17471747
PyObject *etype = NULL, *evalue = NULL, *etrace = NULL;
17481748
PyErr_Fetch(&etype, &evalue, &etrace);
17491749
PyObject *InvalidDocument = _error("InvalidDocument");
1750+
if (InvalidDocument == NULL) {
1751+
return 0;
1752+
}
17501753

17511754
if (top_level && InvalidDocument && PyErr_GivenExceptionMatches(etype, InvalidDocument)) {
17521755

@@ -1760,6 +1763,9 @@ int write_dict(PyObject* self, buffer_t buffer,
17601763
if (msg) {
17611764
// Prepend doc to the existing message
17621765
PyObject *dict_str = PyObject_Str(dict);
1766+
if (dict_str == NULL) {
1767+
return 0;
1768+
}
17631769
PyObject *new_msg = PyUnicode_FromFormat("Invalid document %s | %s", PyUnicode_AsUTF8(dict_str), PyUnicode_AsUTF8(msg));
17641770
Py_DECREF(dict_str);
17651771

0 commit comments

Comments
 (0)