Skip to content

Commit e824c37

Browse files
committed
refactor(test/libc-pipe): replace unsafe read with safe read_into_slice
1 parent 5b858b6 commit e824c37

File tree

2 files changed

+8
-20
lines changed

2 files changed

+8
-20
lines changed

tests/pass-dep/libc/libc-pipe.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,11 @@ fn test_pipe() {
3838
let data = b"123";
3939
write_all_from_slice(fds[1], data).unwrap();
4040
let mut buf4: [u8; 5] = [0; 5];
41-
let res = unsafe { libc::read(fds[0], buf4.as_mut_ptr().cast(), buf4.len() as libc::size_t) };
42-
assert!(res > 0 && res <= 3);
43-
let res = res as usize;
41+
let res = read_into_slice(fds[0], &mut buf4).unwrap().0.len();
4442
assert_eq!(buf4[..res], data[..res]);
4543
if res < 3 {
4644
// Drain the rest from the read end.
47-
let res = unsafe { libc_utils::read_all(fds[0], buf4[res..].as_mut_ptr().cast(), 3 - res) };
45+
let res = read_into_slice(fds[0], &mut buf4[res..]).unwrap().0.len();
4846
assert!(res > 0);
4947
}
5048
}
@@ -64,8 +62,7 @@ fn test_pipe_threaded() {
6462
// Read and write from different direction
6563
let thread2 = thread::spawn(move || {
6664
thread::yield_now();
67-
let data = b"12345";
68-
write_all_from_slice(fds[1], data).unwrap();
65+
write_all_from_slice(fds[1], b"12345").unwrap();
6966
});
7067
let buf = read_all_into_array::<5>(fds[0]).unwrap();
7168
assert_eq!(&buf, b"12345");
@@ -87,8 +84,7 @@ fn test_race() {
8784
unsafe { assert_eq!(VAL, 1) };
8885
});
8986
unsafe { VAL = 1 };
90-
let data = b"a";
91-
write_all_from_slice(fds[1], data).unwrap();
87+
write_all_from_slice(fds[1], b"a").unwrap();
9288
thread::yield_now();
9389
thread1.join().unwrap();
9490
}
@@ -176,8 +172,7 @@ fn test_pipe_fcntl_threaded() {
176172
// The write below will unblock the `read` in main thread: even though
177173
// the socket is now "non-blocking", the shim needs to deal correctly
178174
// with threads that were blocked before the socket was made non-blocking.
179-
let data = b"abcde";
180-
write_all_from_slice(fds[1], data).unwrap();
175+
write_all_from_slice(fds[1], b"abcde").unwrap();
181176
});
182177
// The `read` below will block.
183178
let buf = read_all_into_array::<5>(fds[0]).unwrap();

tests/utils/libc.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,7 @@ pub fn read_all_into_array<const N: usize>(fd: libc::c_int) -> Result<[u8; N], l
5353
}
5454
}
5555

56-
pub fn read_all_into_slice(
57-
fd: libc::c_int,
58-
buf: &mut [u8],
59-
) -> Result<(), libc::ssize_t> {
56+
pub fn read_all_into_slice(fd: libc::c_int, buf: &mut [u8]) -> Result<(), libc::ssize_t> {
6057
let res = unsafe { read_all(fd, buf.as_mut_ptr().cast(), buf.len()) };
6158
if res >= 0 {
6259
assert_eq!(res as usize, buf.len());
@@ -70,12 +67,8 @@ pub fn read_into_slice(
7067
fd: libc::c_int,
7168
buf: &mut [u8],
7269
) -> Result<(&mut [u8], &mut [u8]), libc::ssize_t> {
73-
let res = unsafe { read_all(fd, buf.as_mut_ptr().cast(), buf.len()) };
74-
if res >= 0 {
75-
Ok(buf.split_at_mut(res as usize))
76-
} else {
77-
Err(res)
78-
}
70+
let res = unsafe { libc::read(fd, buf.as_mut_ptr().cast(), buf.len()) };
71+
if res >= 0 { Ok(buf.split_at_mut(res as usize)) } else { Err(res) }
7972
}
8073

8174
pub unsafe fn write_all(

0 commit comments

Comments
 (0)