From 8b3a3bebe79d9bdff2e97af755014350706d1fb4 Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Wed, 5 Apr 2023 13:07:10 +0200 Subject: [PATCH] Fix list::remove when trying to remove first element of the list (SIGSEGV) --- src/kernel/thread_manager.rs | 2 ++ src/utility/list.rs | 24 ++++++++++++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/kernel/thread_manager.rs b/src/kernel/thread_manager.rs index 3b558bc..e7b1288 100644 --- a/src/kernel/thread_manager.rs +++ b/src/kernel/thread_manager.rs @@ -141,6 +141,8 @@ impl ThreadManager { let old_status = machine.interrupt.set_status(InterruptStatus::InterruptOff); self.g_thread_to_be_destroyed = Option::Some(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 self.thread_sleep(machine, Rc::clone(&thread)); machine.interrupt.set_status(old_status); diff --git a/src/utility/list.rs b/src/utility/list.rs index 801a74e..db514de 100644 --- a/src/utility/list.rs +++ b/src/utility/list.rs @@ -113,8 +113,12 @@ impl List { let mut current: *mut Node = self.head; let mut previous: *mut Node = ptr::null_mut(); while !current.is_null() { - if (*current).elem == item { - (*previous).next = (*current).next; + if (&*current).elem == item { + if !previous.is_null() { + (*previous).next = (*current).next; + } else { + self.head = (*current).next; + } drop(Box::from_raw(current).elem); return true; } else { @@ -320,6 +324,22 @@ mod test { 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] fn miri_test() { let mut list = List::default();