Skip to content

Commit

Permalink
Fix FdList::remove crashing when called with bad index
Browse files Browse the repository at this point in the history
  • Loading branch information
Arshia001 committed Nov 19, 2024
1 parent 408ce50 commit 5ef1671
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion lib/wasix/src/fs/fd_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ impl FdList {
pub fn remove(&mut self, idx: WasiFd) -> Option<Fd> {
let idx = idx as usize;

let result = self.fds[idx].take();
let result = self.fds.get_mut(idx).and_then(|fd| fd.take());

if result.is_some() {
match self.first_free {
Expand Down Expand Up @@ -401,6 +401,19 @@ mod tests {
assert_eq!(l.first_free, Some(2));
}

#[test]
fn remove_works() {
let mut l = FdList::new();

l.insert_first_free(useless_fd(0));
l.insert_first_free(useless_fd(1));
l.insert_first_free(useless_fd(2));

assert!(is_useless_fd(&l.remove(1).unwrap(), 1));
assert!(l.remove(1).is_none());
assert!(l.remove(100000).is_none());
}

#[test]
fn clear_works() {
let mut l = FdList::new();
Expand Down

0 comments on commit 5ef1671

Please sign in to comment.