Skip to content

Commit 26d5417

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

File tree

2 files changed

+6
-11
lines changed

2 files changed

+6
-11
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ pub fn read_into_slice(
7070
fd: libc::c_int,
7171
buf: &mut [u8],
7272
) -> Result<(&mut [u8], &mut [u8]), libc::ssize_t> {
73-
let res = unsafe { read_all(fd, buf.as_mut_ptr().cast(), buf.len()) };
73+
let res = unsafe { libc::read(fd, buf.as_mut_ptr().cast(), buf.len()) };
7474
if res >= 0 {
7575
Ok(buf.split_at_mut(res as usize))
7676
} else {

0 commit comments

Comments
 (0)