xhcid: P0-A4 — bounds-check root_hub_port_index() calls

Replace 5 bare unwrap() / index-operator sites on root_hub_port_index()
with bounded access:
  get_pls():    expect() with diagnostic (returns u8, can't use ?)
  poll():       match None → continue
  print_port:   match None → continue
  reset_port(): ok_or_else(|| Error::new(EINVAL))? (returns Result)
  attach:       ok_or_else(|| Error::new(EINVAL))? (returns Result)

Added EINVAL to syscall::error import.
This commit is contained in:
Red Bear OS
2026-07-07 02:07:46 +03:00
parent 7cfed158b8
commit 774a0ac118
+16 -6
View File
@@ -16,7 +16,7 @@ use std::sync::atomic::AtomicUsize;
use std::sync::{Arc, Mutex};
use std::{mem, process, slice, thread};
use syscall::error::{Error, Result, EBADF, EBADMSG, EIO, ENOENT};
use syscall::error::{Error, Result, EBADF, EBADMSG, EINVAL, EIO, ENOENT};
use syscall::{EAGAIN, PAGE_SIZE};
use chashmap::CHashMap;
@@ -600,7 +600,8 @@ impl<const N: usize> Xhci<N> {
pub fn get_pls(&self, port_id: PortId) -> u8 {
let mut ports = self.ports.lock().unwrap();
let port = ports.get_mut(port_id.root_hub_port_index()).unwrap();
let port = ports.get_mut(port_id.root_hub_port_index())
.expect("get_pls: port index out of range");
port.state()
}
@@ -618,7 +619,10 @@ impl<const N: usize> Xhci<N> {
//Get the CCS and CSC flags
let (ccs, csc, flags) = {
let mut ports = self.ports.lock().unwrap();
let port = &mut ports[port_id.root_hub_port_index()];
let port = match ports.get_mut(port_id.root_hub_port_index()) {
Some(p) => p,
None => continue,
};
let flags = port.flags();
let ccs = flags.contains(PortFlags::CCS);
let csc = flags.contains(PortFlags::CSC);
@@ -660,7 +664,10 @@ impl<const N: usize> Xhci<N> {
{
let mut ports = self.ports.lock().unwrap();
flags = ports[port_id.root_hub_port_index()].flags();
flags = match ports.get(port_id.root_hub_port_index()) {
Some(p) => p.flags(),
None => continue,
};
}
match self.supported_protocol(port_id) {
@@ -686,7 +693,8 @@ impl<const N: usize> Xhci<N> {
//TODO handle the second unwrap
let mut ports = self.ports.lock().unwrap();
let port = ports.get_mut(port_id.root_hub_port_index()).unwrap();
let port = ports.get_mut(port_id.root_hub_port_index())
.ok_or_else(|| Error::new(EINVAL))?;
let instant = std::time::Instant::now();
debug!("Port {} Link State: {}", port_id, port.state());
@@ -799,7 +807,9 @@ impl<const N: usize> Xhci<N> {
}
let (data, state, speed, flags) = {
let port = &self.ports.lock().unwrap()[port_id.root_hub_port_index()];
let port = self.ports.lock().unwrap();
let port = port.get(port_id.root_hub_port_index())
.ok_or_else(|| Error::new(EINVAL))?;
(port.read(), port.state(), port.speed(), port.flags())
};