Skip to content

Commit

Permalink
Fix Potential Nullptr Dereference
Browse files Browse the repository at this point in the history
Fix a potential nullptr dereference of `obj`.

Found with coverity in a downstream project.
  • Loading branch information
ax3l committed Jan 8, 2019
1 parent 085a294 commit 0154597
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions include/pybind11/detail/class.h
Original file line number Diff line number Diff line change
Expand Up @@ -461,17 +461,21 @@ inline void enable_dynamic_attributes(PyHeapTypeObject *heap_type) {

/// buffer_protocol: Fill in the view as specified by flags.
extern "C" inline int pybind11_getbuffer(PyObject *obj, Py_buffer *view, int flags) {
if (view == nullptr || obj == nullptr) {
if (view)
view->obj = nullptr;
PyErr_SetString(PyExc_BufferError, "pybind11_getbuffer(): Internal error (nullptr passed)");
return -1;
}
// Look for a `get_buffer` implementation in this type's info or any bases (following MRO).
type_info *tinfo = nullptr;
for (auto type : reinterpret_borrow<tuple>(Py_TYPE(obj)->tp_mro)) {
tinfo = get_type_info((PyTypeObject *) type.ptr());
if (tinfo && tinfo->get_buffer)
break;
}
if (view == nullptr || obj == nullptr || !tinfo || !tinfo->get_buffer) {
if (view)
view->obj = nullptr;
PyErr_SetString(PyExc_BufferError, "pybind11_getbuffer(): Internal error");
if (!tinfo || !tinfo->get_buffer) {
PyErr_SetString(PyExc_BufferError, "pybind11_getbuffer(): Internal error (get_type_info)");
return -1;
}
std::memset(view, 0, sizeof(Py_buffer));
Expand Down

0 comments on commit 0154597

Please sign in to comment.