-
Notifications
You must be signed in to change notification settings - Fork 13.8k
Ensure non-empty buffers for large vectored I/O #138879
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,32 @@ | ||
use core::mem::ManuallyDrop; | ||
|
||
use super::FileDesc; | ||
use super::{FileDesc, max_iov}; | ||
use crate::io::IoSlice; | ||
use crate::os::unix::io::FromRawFd; | ||
|
||
#[test] | ||
fn limit_vector_count() { | ||
const IOV_MAX: usize = max_iov(); | ||
|
||
let stdout = ManuallyDrop::new(unsafe { FileDesc::from_raw_fd(1) }); | ||
let mut bufs = vec![IoSlice::new(&[]); IOV_MAX * 2 + 1]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that IOV_MAX can be pretty large in theory. Maybe it would be good to panic on platforms where the value is large (tens of Mb)? That would give us a heads up to skip the test on platforms where the allocation would eat a chunk of memory or possibly OOM (rather than looking like a spurious failure). |
||
assert_eq!(stdout.write_vectored(&bufs).unwrap(), 0); | ||
|
||
// The slice of buffers is truncated to IOV_MAX buffers. However, since the | ||
// first IOV_MAX buffers are all empty, it is sliced starting at the first | ||
// non-empty buffer to avoid erroneously returning Ok(0). In this case, that | ||
// starts with the b"hello" buffer and ends just before the b"world!" | ||
// buffer. | ||
bufs[IOV_MAX] = IoSlice::new(b"hello"); | ||
bufs[IOV_MAX * 2] = IoSlice::new(b"world!"); | ||
assert_eq!(stdout.write_vectored(&bufs).unwrap(), b"hello".len()) | ||
} | ||
|
||
#[test] | ||
fn empty_vector() { | ||
let stdin = ManuallyDrop::new(unsafe { FileDesc::from_raw_fd(0) }); | ||
assert_eq!(stdin.read_vectored(&mut []).unwrap(), 0); | ||
|
||
let stdout = ManuallyDrop::new(unsafe { FileDesc::from_raw_fd(1) }); | ||
let bufs = (0..1500).map(|_| IoSlice::new(&[])).collect::<Vec<_>>(); | ||
assert!(stdout.write_vectored(&bufs).is_ok()); | ||
assert_eq!(stdout.write_vectored(&[]).unwrap(), 0); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could the Windows tests be updated too?