diff --git a/include/pybind11/detail/class.h b/include/pybind11/detail/class.h index 90318dbdca9..4b7ad4bdaaa 100644 --- a/include/pybind11/detail/class.h +++ b/include/pybind11/detail/class.h @@ -603,7 +603,6 @@ extern "C" inline int pybind11_getbuffer(PyObject *obj, Py_buffer *view, int fla // Fill in all the information, and then downgrade as requested by the caller, or raise an // error if that's not possible. - view->obj = obj; view->buf = info->ptr; view->itemsize = info->itemsize; view->len = view->itemsize; @@ -659,7 +658,10 @@ extern "C" inline int pybind11_getbuffer(PyObject *obj, Py_buffer *view, int fla } } + // Set these after all checks so they don't leak out into the caller, and can be automatically + // cleaned up on error. view->internal = info.release(); + view->obj = obj; Py_INCREF(view->obj); return 0; }