Fix list::remove when trying to remove first element of the list (SIGSEGV)

This commit is contained in:
Quentin Legot 2023-04-05 13:07:10 +02:00
parent 24be35547e
commit 8b3a3bebe7
2 changed files with 24 additions and 2 deletions

View File

@ -141,6 +141,8 @@ impl ThreadManager {
let old_status = machine.interrupt.set_status(InterruptStatus::InterruptOff); let old_status = machine.interrupt.set_status(InterruptStatus::InterruptOff);
self.g_thread_to_be_destroyed = Option::Some(Rc::clone(&thread)); self.g_thread_to_be_destroyed = Option::Some(Rc::clone(&thread));
self.g_alive.remove(Rc::clone(&thread)); self.g_alive.remove(Rc::clone(&thread));
#[cfg(debug_assertions)]
println!("Sleeping thread {}", thread.borrow().get_name());
// g_objets_addrs->removeObject(self.thread) // a ajouté plus tard // g_objets_addrs->removeObject(self.thread) // a ajouté plus tard
self.thread_sleep(machine, Rc::clone(&thread)); self.thread_sleep(machine, Rc::clone(&thread));
machine.interrupt.set_status(old_status); machine.interrupt.set_status(old_status);

View File

@ -113,8 +113,12 @@ impl<T: PartialEq> List<T> {
let mut current: *mut Node<T> = self.head; let mut current: *mut Node<T> = self.head;
let mut previous: *mut Node<T> = ptr::null_mut(); let mut previous: *mut Node<T> = ptr::null_mut();
while !current.is_null() { while !current.is_null() {
if (*current).elem == item { if (&*current).elem == item {
if !previous.is_null() {
(*previous).next = (*current).next; (*previous).next = (*current).next;
} else {
self.head = (*current).next;
}
drop(Box::from_raw(current).elem); drop(Box::from_raw(current).elem);
return true; return true;
} else { } else {
@ -320,6 +324,22 @@ mod test {
assert_eq!(list.peek(), Option::None); assert_eq!(list.peek(), Option::None);
} }
#[test]
fn remove_test2() {
let mut list = List::default();
assert_eq!(list.peek(), None);
list.push(1);
list.push(2);
list.push(3);
assert_eq!(list.contains(&1), true);
list.remove(1);
assert_eq!(list.contains(&1), false);
assert_eq!(list.pop(), Option::Some(2));
assert_eq!(list.pop(), Option::Some(3));
assert_eq!(list.peek(), Option::None);
}
#[test] #[test]
fn miri_test() { fn miri_test() {
let mut list = List::default(); let mut list = List::default();