Move the maxevents.try_into().unwrap() after value check

This commit is contained in:
tiif 2024-08-18 13:36:56 +08:00
parent c0e799db1a
commit 8ae118dc0a
2 changed files with 9 additions and 8 deletions

View File

@ -403,18 +403,20 @@ fn epoll_wait(
let epfd = this.read_scalar(epfd)?.to_i32()?;
let maxevents = this.read_scalar(maxevents)?.to_i32()?;
let event = this.deref_pointer_as(
events_op,
this.libc_array_ty_layout("epoll_event", maxevents.try_into().unwrap()),
)?;
let timeout = this.read_scalar(timeout)?.to_i32()?;
if epfd <= 0 || maxevents <= 0 {
let einval = this.eval_libc("EINVAL");
this.set_last_error(einval)?;
return Ok(Scalar::from_i32(-1));
}
// This needs to come after the maxevents value check, or else maxevents.try_into().unwrap()
// will fail.
let event = this.deref_pointer_as(
events_op,
this.libc_array_ty_layout("epoll_event", maxevents.try_into().unwrap()),
)?;
// FIXME: Implement blocking support
if timeout != 0 {
throw_unsup_format!("epoll_wait: timeout value can only be 0");

View File

@ -19,7 +19,7 @@ fn main() {
test_epoll_ctl_del();
test_pointer();
test_two_same_fd_in_same_epoll_instance();
test_epoll_wait_less_maxevent_zero();
test_epoll_wait_maxevent_zero();
}
// Using `as` cast since `EPOLLET` wraps around
@ -530,8 +530,7 @@ fn test_no_notification_for_unregister_flag() {
check_epoll_wait::<8>(epfd, &[(expected_event, expected_value)]);
}
fn test_epoll_wait_less_maxevent_zero() {
fn test_epoll_wait_maxevent_zero() {
// Create an epoll instance.
let epfd = unsafe { libc::epoll_create1(0) };
assert_ne!(epfd, -1);