Skip to content

Fix file descriptor ranges for fuchsia #81226

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions library/std/src/sys/unix/fd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ use crate::sys_common::AsInner;
use libc::{c_int, c_void};

#[derive(Debug)]
#[rustc_layout_scalar_valid_range_start(0)]
#[cfg_attr(not(target_os = "fuchsia"), rustc_layout_scalar_valid_range_start(0))]
// libstd/os/raw/mod.rs assures me that every libstd-supported platform has a
// 32-bit c_int. Below is -2, in two's complement, but that only works out
// because c_int is 32 bits.
#[rustc_layout_scalar_valid_range_end(0xFF_FF_FF_FE)]
#[cfg_attr(not(target_os = "fuchsia"), rustc_layout_scalar_valid_range_end(0xFF_FF_FF_FE))]
pub struct FileDesc {
fd: c_int,
}
Expand Down Expand Up @@ -68,7 +68,10 @@ const fn max_iov() -> usize {

impl FileDesc {
pub fn new(fd: c_int) -> FileDesc {
assert_ne!(fd, -1i32);
if cfg!(not(target_os = "fuchsia")) {
assert_ne!(fd, -1i32);
}

// SAFETY: we just asserted that the value is in the valid range and isn't `-1` (the only value bigger than `0xFF_FF_FF_FE` unsigned)
unsafe { FileDesc { fd } }
}
Expand Down
7 changes: 6 additions & 1 deletion library/std/src/sys/unix/fd/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ use core::mem::ManuallyDrop;

#[test]
fn limit_vector_count() {
let stdout = ManuallyDrop::new(unsafe { FileDesc { fd: 1 } });
let stdout = ManuallyDrop::new(
#[cfg_attr(target_os = "fuchsia", allow(unused_unsafe))]
unsafe {
FileDesc { fd: 1 }
},
);
let bufs = (0..1500).map(|_| IoSlice::new(&[])).collect::<Vec<_>>();
assert!(stdout.write_vectored(&bufs).is_ok());
}