Skip to content

Commit

Permalink
Check to see if object is None before traversing
Browse files Browse the repository at this point in the history
Closes PyO3#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.
  • Loading branch information
neachdainn committed Jan 26, 2023
1 parent 9004bd2 commit 1bb6edb
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/pyclass/gc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
}

Expand Down

0 comments on commit 1bb6edb

Please sign in to comment.