Skip to content

Commit

Permalink
PyList: fix segfault on get_item with negative indices
Browse files Browse the repository at this point in the history
See #1667.
  • Loading branch information
birkenfeld committed Jun 6, 2021
1 parent 6b68114 commit cb6dedb
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/types/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl PyList {
///
/// Panics if the index is out of range.
pub fn get_item(&self, index: isize) -> &PyAny {
assert!((index.abs() as usize) < self.len());
assert!(index >= 0 && index < self.len() as isize);
unsafe {
#[cfg(not(Py_LIMITED_API))]
let ptr = ffi::PyList_GET_ITEM(self.as_ptr(), index as Py_ssize_t);
Expand Down Expand Up @@ -237,6 +237,15 @@ mod test {
assert_eq!(7, list.get_item(3).extract::<i32>().unwrap());
}

#[test]
#[should_panic]
fn test_get_item_invalid() {
let gil = Python::acquire_gil();
let py = gil.python();
let list = PyList::new(py, [2, 3, 5, 7]);
list.get_item(-1);
}

#[test]
fn test_set_item() {
let gil = Python::acquire_gil();
Expand Down

0 comments on commit cb6dedb

Please sign in to comment.