From 95aaef9aa1ff718275f4f52b50a4134694a08b00 Mon Sep 17 00:00:00 2001 From: Marsman1996 Date: Wed, 21 Aug 2024 15:13:29 +0800 Subject: [PATCH] Check the length of timers before delete --- .../aster-nix/src/process/process/timer_manager.rs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/kernel/aster-nix/src/process/process/timer_manager.rs b/kernel/aster-nix/src/process/process/timer_manager.rs index 0815a59d4..3a071d309 100644 --- a/kernel/aster-nix/src/process/process/timer_manager.rs +++ b/kernel/aster-nix/src/process/process/timer_manager.rs @@ -198,11 +198,15 @@ impl PosixTimerManager { /// Removes the POSIX timer with the ID `timer_id`. pub fn remove_posix_timer(&self, timer_id: usize) -> Option> { let mut timers = self.posix_timers.lock(); - let timer = timers[timer_id].take(); - if timer.is_some() { - // Holding the lock of `posix_timers` is required to operate the `id_allocator`. - self.id_allocator.lock().free(timer_id); + if timer_id < timers.len() { + let timer = timers[timer_id].take(); + if timer.is_some() { + // Holding the lock of `posix_timers` is required to operate the `id_allocator`. + self.id_allocator.lock().free(timer_id); + } + timer + } else { + None } - timer } }