-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Fix invalid access when reinterpret_casting a non-pybind11 PyObject* to instance* (found by Valgrind in #2746) #2755
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
Fix invalid access when reinterpret_casting a non-pybind11 PyObject* to instance* (found by Valgrind in #2746) #2755
Conversation
…to instance* (found by Valgrind in pybind#2746)
@@ -504,15 +504,15 @@ class cpp_function : public function { | |||
|
|||
auto self_value_and_holder = value_and_holder(); | |||
if (overloads->is_constructor) { | |||
const auto tinfo = get_type_info((PyTypeObject *) overloads->scope.ptr()); | |||
const auto pi = reinterpret_cast<instance *>(parent.ptr()); |
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.
Comments from #2746:
- @bstaletic: Without checking the type first, we were reinterpreting
NotPybindDerived()
asinstance*
. - @YannickJadoul: See also the code below that gets simplified; cfr.
if (!self_value_and_holder.type || !self_value_and_holder.inst) -> pi->get_value_and_holder(tinfo, true);
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 believe so. At any rate, I'll take responsibility for rebasing :-) |
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.
Looks great! I'm assuming there may be a bit of performance penalty, but nothing that can't be resolved later.
Mayhaps there's a way to fold this fail-fast more directly into get_type_info
, but this is def. a strict improvement!
Yes. But as fa as I know, the same penalty as for a normal cast, if it would not have been |
Thanks all. Since it was already tested in #2746, I'll merge this! :-) |
@EricCousineau-TRI, there's a bit of optimization in pybind11/include/pybind11/cast.h Lines 675 to 713 in e612043
Worth checking out, indeed. Do we create an issue for this? |
Nah, just speculative at this point. I think it only merits an issue if there's measurable / traceable performance penalties. (I'll keep in mind to try benchmarking older versions, say 2.3.x, to get some semblance of history, per #2760) Also, not sure if I understand how (FWIW, I don't think this is too actionable yet, so we can let sleeping dogs lie 🐶 💤) |
Maybe it's not, though; I saw this |
Description
Carved out from #2746 to ease reviewing process.
Suggested changelog entry:
Fix invalid access when calling a pybind11 ``__init__`` on a non-pybind11 class instance.