Check to see if object is None
before traversing
Closes #2915 When using the C API directly, the intended way to call `visitproc` is via the `Py_VISIT` macro, which checks to see that the provided pointer is not null before passing it along to `visitproc`. Because PyO3 isn't using the macro, it needs to manually check that the pointer isn't null. Without this check, calling `visit.call(&obj)` where `let obj = None;` will segfault.
This commit is contained in:
parent
98b12973fc
commit
f38841a8e2
1
newsfragments/2921.fixed.md
Normal file
1
newsfragments/2921.fixed.md
Normal file
|
@ -0,0 +1 @@
|
|||
Traversal visit calls to `Option<T: AsPyPointer>` no longer segfaults when `None`.
|
|
@ -23,11 +23,16 @@ impl<'p> PyVisit<'p> {
|
|||
where
|
||||
T: AsPyPointer,
|
||||
{
|
||||
let r = unsafe { (self.visit)(obj.as_ptr(), self.arg) };
|
||||
if r == 0 {
|
||||
Ok(())
|
||||
let ptr = obj.as_ptr();
|
||||
if !ptr.is_null() {
|
||||
let r = unsafe { (self.visit)(ptr, self.arg) };
|
||||
if r == 0 {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(PyTraverseError(r))
|
||||
}
|
||||
} else {
|
||||
Err(PyTraverseError(r))
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue