-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PYTHON-5013 Add NULL checks in InvalidDocument bson handling #2049
base: master
Are you sure you want to change the base?
Conversation
bson/_cbsonmodule.c
Outdated
@@ -1747,6 +1747,9 @@ int write_dict(PyObject* self, buffer_t buffer, | |||
PyObject *etype = NULL, *evalue = NULL, *etrace = NULL; | |||
PyErr_Fetch(&etype, &evalue, &etrace); | |||
PyObject *InvalidDocument = _error("InvalidDocument"); | |||
if (InvalidDocument == NULL) { | |||
return 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This return case will leak &etype, &evalue, &etrace
. We either need to call PyErr_Restore or xdecref those explicitly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
bson/_cbsonmodule.c
Outdated
@@ -1760,6 +1763,9 @@ int write_dict(PyObject* self, buffer_t buffer, | |||
if (msg) { | |||
// Prepend doc to the existing message | |||
PyObject *dict_str = PyObject_Str(dict); | |||
if (dict_str == NULL) { | |||
return 0; | |||
} | |||
PyObject *new_msg = PyUnicode_FromFormat("Invalid document %s | %s", PyUnicode_AsUTF8(dict_str), PyUnicode_AsUTF8(msg)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you fix the indentation on line 1776?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also I believe PyUnicode_AsUTF8 can return NULL on error so we'll need checks there too. Unless there's a simpler API to construct this string that does the NULL checks for us.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
bson/_cbsonmodule.c
Outdated
@@ -1760,6 +1763,9 @@ int write_dict(PyObject* self, buffer_t buffer, | |||
if (msg) { | |||
// Prepend doc to the existing message | |||
PyObject *dict_str = PyObject_Str(dict); | |||
if (dict_str == NULL) { | |||
return 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same leak comment here except we'll also leak more variables, eg msg.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
bson/_cbsonmodule.c
Outdated
@@ -1747,6 +1747,9 @@ int write_dict(PyObject* self, buffer_t buffer, | |||
PyObject *etype = NULL, *evalue = NULL, *etrace = NULL; | |||
PyErr_Fetch(&etype, &evalue, &etrace); | |||
PyObject *InvalidDocument = _error("InvalidDocument"); | |||
if (InvalidDocument == NULL) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just realized there's a larger problem here. This logic is only in place for the is_dict
fast path where it should be added generically for any mapping.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, I'll see if I can factor this out.
No description provided.