Skip to content
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 NULL pointer deref when creating iterator for non-iterable #558

Merged
merged 1 commit into from
Aug 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/types/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ impl<'p> PyIterator<'p> {
{
unsafe {
let ptr = ffi::PyObject_GetIter(obj.as_ptr());
// Returns NULL if an object cannot be iterated.
if ptr.is_null() {
return Err(PyDowncastError);
}

if ffi::PyIter_Check(ptr) != 0 {
// this is not right, but this cause of segfault check #71
Expand Down
10 changes: 10 additions & 0 deletions tests/test_various.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,13 @@ fn test_pickle() {
"#
);
}

#[test]
fn incorrect_iter() {
let gil = Python::acquire_gil();
let py = gil.python();
let int = 13isize.to_object(py);
let int_ref = int.as_ref(py);
// Should not segfault.
assert!(int_ref.iter().is_err());
}