Fix panic in `add_posix_timer`

This commit is contained in:
Marsman1996 2026-01-11 23:22:31 +08:00
parent 184e803869
commit 73cf1b1686
2 changed files with 8 additions and 6 deletions

View File

@ -175,18 +175,18 @@ impl PosixTimerManager {
}
/// Adds a POSIX timer to the managed `posix_timers`, and allocate a timer ID for this timer.
/// Return the timer ID.
pub fn add_posix_timer(&self, posix_timer: Arc<Timer>) -> usize {
/// Return the timer ID, or `None` if allocation failed.
pub fn add_posix_timer(&self, posix_timer: Arc<Timer>) -> Option<usize> {
let mut timers = self.posix_timers.lock();
// Holding the lock of `posix_timers` is required to operate the `id_allocator`.
let timer_id = self.id_allocator.lock().alloc().unwrap();
if timers.len() < timer_id + 1 {
let timer_id = self.id_allocator.lock().alloc()?;
if timers.len() <= timer_id {
timers.resize(timer_id + 1, None);
}
// The ID allocated is not used by any other timers so this index in `timers`
// must be `None`.
timers[timer_id] = Some(posix_timer);
timer_id
Some(timer_id)
}
/// Finds a POSIX timer by the input `timer_id`.

View File

@ -108,7 +108,9 @@ pub fn sys_timer_create(
let timer = create_timer(clockid, func, ctx)?;
let timer_id = current_process.timer_manager().add_posix_timer(timer);
let Some(timer_id) = current_process.timer_manager().add_posix_timer(timer) else {
return_errno_with_message!(Errno::EAGAIN, "timer IDs are exhausted");
};
ctx.user_space().write_val(timer_id_addr, &timer_id)?;
Ok(SyscallReturn::Return(0))
}