|
| 1 | +#include "pybind11_tests.h" |
| 2 | + |
| 3 | +#include <Python.h> |
| 4 | +#include <memory> |
| 5 | +#include <vector> |
| 6 | + |
| 7 | +class VectorOwns4PythonObjects { |
| 8 | +public: |
| 9 | + void append(const py::object &obj) { |
| 10 | + if (size() >= 4) { |
| 11 | + throw std::out_of_range("Index out of range"); |
| 12 | + } |
| 13 | + vec.emplace_back(obj); |
| 14 | + } |
| 15 | + |
| 16 | + void set_item(py::ssize_t i, const py::object &obj) { |
| 17 | + if (!(i >= 0 && i < size())) { |
| 18 | + throw std::out_of_range("Index out of range"); |
| 19 | + } |
| 20 | + vec[py::size_t(i)] = obj; |
| 21 | + } |
| 22 | + |
| 23 | + py::object get_item(py::ssize_t i) const { |
| 24 | + if (!(i >= 0 && i < size())) { |
| 25 | + throw std::out_of_range("Index out of range"); |
| 26 | + } |
| 27 | + return vec[py::size_t(i)]; |
| 28 | + } |
| 29 | + |
| 30 | + py::ssize_t size() const { return py::ssize_t_cast(vec.size()); } |
| 31 | + |
| 32 | + bool is_empty() const { return vec.empty(); } |
| 33 | + |
| 34 | + void sanity_check() const { |
| 35 | + auto current_size = size(); |
| 36 | + if (current_size < 0 || current_size > 4) { |
| 37 | + throw std::out_of_range("Invalid size"); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + static int tp_traverse(PyObject *self_base, visitproc visit, void *arg) { |
| 42 | +#if PY_VERSION_HEX >= 0x03090000 // Python 3.9 |
| 43 | + Py_VISIT(Py_TYPE(self_base)); |
| 44 | +#endif |
| 45 | + auto *const instance = reinterpret_cast<py::detail::instance *>(self_base); |
| 46 | + if (!instance->get_value_and_holder().holder_constructed()) { |
| 47 | + // The holder has not been constructed yet. Skip the traversal to avoid segmentation |
| 48 | + // faults. |
| 49 | + return 0; |
| 50 | + } |
| 51 | + auto &self = py::cast<VectorOwns4PythonObjects &>(py::handle{self_base}); |
| 52 | + for (const auto &obj : self.vec) { |
| 53 | + Py_VISIT(obj.ptr()); |
| 54 | + } |
| 55 | + return 0; |
| 56 | + } |
| 57 | + |
| 58 | +private: |
| 59 | + std::vector<py::object> vec{}; |
| 60 | +}; |
| 61 | + |
| 62 | +TEST_SUBMODULE(invalid_holder_access, m) { |
| 63 | + m.doc() = "Test invalid holder access"; |
| 64 | + |
| 65 | +#if defined(PYBIND11_CPP14) |
| 66 | + m.def("create_vector", []() -> std::unique_ptr<VectorOwns4PythonObjects> { |
| 67 | + auto vec = std::make_unique<VectorOwns4PythonObjects>(); |
| 68 | + vec->append(py::none()); |
| 69 | + vec->append(py::int_(1)); |
| 70 | + vec->append(py::str("test")); |
| 71 | + vec->append(py::tuple()); |
| 72 | + return vec; |
| 73 | + }); |
| 74 | +#endif |
| 75 | + |
| 76 | + py::class_<VectorOwns4PythonObjects>( |
| 77 | + m, |
| 78 | + "VectorOwns4PythonObjects", |
| 79 | + py::custom_type_setup([](PyHeapTypeObject *heap_type) -> void { |
| 80 | + auto *const type = &heap_type->ht_type; |
| 81 | + type->tp_flags |= Py_TPFLAGS_HAVE_GC; |
| 82 | + type->tp_traverse = &VectorOwns4PythonObjects::tp_traverse; |
| 83 | + })) |
| 84 | + .def("append", &VectorOwns4PythonObjects::append, py::arg("obj")) |
| 85 | + .def("set_item", &VectorOwns4PythonObjects::set_item, py::arg("i"), py::arg("obj")) |
| 86 | + .def("get_item", &VectorOwns4PythonObjects::get_item, py::arg("i")) |
| 87 | + .def("size", &VectorOwns4PythonObjects::size) |
| 88 | + .def("is_empty", &VectorOwns4PythonObjects::is_empty) |
| 89 | + .def("__setitem__", &VectorOwns4PythonObjects::set_item, py::arg("i"), py::arg("obj")) |
| 90 | + .def("__getitem__", &VectorOwns4PythonObjects::get_item, py::arg("i")) |
| 91 | + .def("__len__", &VectorOwns4PythonObjects::size) |
| 92 | + .def("sanity_check", &VectorOwns4PythonObjects::sanity_check); |
| 93 | +} |
0 commit comments