Visitar URL original
Removing list with index 0 causes panicked. · Issue #4315 · RustPython/RustPython · GitHub
The Wayback Machine - https://web.archive.org/web/20221229100759/https://github.com/RustPython/RustPython/issues/4315
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

Removing list with index 0 causes panicked. #4315

Open
xiaxinmeng opened this issue Dec 8, 2022 · 2 comments
Open

Removing list with index 0 causes panicked. #4315

xiaxinmeng opened this issue Dec 8, 2022 · 2 comments

Comments

@xiaxinmeng
Copy link

xiaxinmeng commented Dec 8, 2022

In the following example, we define a list1 with a class instance, then we remove the list1, rustpython is panicked.

test.py:


class rewrite_list_eq(list):
	pass

class poc:
    def __eq__(self, other):
        list1.clear()
        return self
list1 = rewrite_list_eq([poc()])
list1.remove(list1)

Behavior on rustpython(rustpython 0.1.2):

thread 'main' panicked at 'removal index (is 0) should be < len (is 0)', vm/src/builtins/list.rs:303:38
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Expected behavior(behavior on CPython 3.9.1):
work well on the Python interpreter.

Step to reproduce:
type "cargo run --release test.py" on the console of Ubuntu 18.04 and MacOS Big Sur 11.7.1
rust version: rustc 1.65.0

@DimitrisJim
Copy link
Member

DimitrisJim commented Dec 8, 2022

Note to anyone wanting to tackle this, test similar to this exist in test_equal_operator_modifying_operand and test_count_index_remove_crashes of test_list.

@yt2b
Copy link
Contributor

yt2b commented Dec 10, 2022 •

I investigated the source code.

fn remove(&self, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
let index = self.mut_index(vm, &needle)?;
if let Some(index) = index.into() {
// defer delete out of borrow
Ok(self.borrow_vec_mut().remove(index))

After calling self.mut_index, an elements of list has been deleted.
then self.borrow_vec_mut().remove is called.
But list is empty, So panic occured.

Checking list size is needed After calling self.mut_index.
I'll fix PyList.remove .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants