From b692fcbb4bd60691af415d42b3e58fe5d469a619 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sat, 3 Jan 2026 16:23:55 +0100 Subject: [PATCH] minor tweaks --- .../miri/tests/pass-dep/libc/libc-pipe.rs | 12 ++++------ src/tools/miri/tests/utils/libc.rs | 24 +++++++++---------- 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/src/tools/miri/tests/pass-dep/libc/libc-pipe.rs b/src/tools/miri/tests/pass-dep/libc/libc-pipe.rs index 102bcbb34a26..1eef8eaf4452 100644 --- a/src/tools/miri/tests/pass-dep/libc/libc-pipe.rs +++ b/src/tools/miri/tests/pass-dep/libc/libc-pipe.rs @@ -38,13 +38,11 @@ fn test_pipe() { let data = b"123"; write_all_from_slice(fds[1], data).unwrap(); let mut buf4: [u8; 5] = [0; 5]; - let res = read_into_slice(fds[0], &mut buf4).unwrap().0.len(); - assert_eq!(buf4[..res], data[..res]); - if res < 3 { - // Drain the rest from the read end. - let res = read_into_slice(fds[0], &mut buf4[res..]).unwrap().0.len(); - assert!(res > 0); - } + let (part1, rest) = read_into_slice(fds[0], &mut buf4).unwrap(); + assert_eq!(part1[..], data[..part1.len()]); + // Write 2 more bytes so we can exactly fill the `rest`. + write_all_from_slice(fds[1], b"34").unwrap(); + read_all_into_slice(fds[0], rest).unwrap(); } fn test_pipe_threaded() { diff --git a/src/tools/miri/tests/utils/libc.rs b/src/tools/miri/tests/utils/libc.rs index 114aade7d31d..0765bacb6bd8 100644 --- a/src/tools/miri/tests/utils/libc.rs +++ b/src/tools/miri/tests/utils/libc.rs @@ -40,19 +40,8 @@ pub unsafe fn read_all( return read_so_far as libc::ssize_t; } -/// Read exactly `N` bytes from `fd`. Error if that many bytes could not be read. +/// Try to fill the given slice by reading from `fd`. Error if that many bytes could not be read. #[track_caller] -pub fn read_all_into_array(fd: libc::c_int) -> Result<[u8; N], libc::ssize_t> { - let mut buf = [0; N]; - let res = unsafe { read_all(fd, buf.as_mut_ptr().cast(), buf.len()) }; - if res >= 0 { - assert_eq!(res as usize, buf.len()); - Ok(buf) - } else { - Err(res) - } -} - pub fn read_all_into_slice(fd: libc::c_int, buf: &mut [u8]) -> Result<(), libc::ssize_t> { let res = unsafe { read_all(fd, buf.as_mut_ptr().cast(), buf.len()) }; if res >= 0 { @@ -63,6 +52,17 @@ pub fn read_all_into_slice(fd: libc::c_int, buf: &mut [u8]) -> Result<(), libc:: } } +/// Read exactly `N` bytes from `fd`. Error if that many bytes could not be read. +#[track_caller] +pub fn read_all_into_array(fd: libc::c_int) -> Result<[u8; N], libc::ssize_t> { + let mut buf = [0; N]; + read_all_into_slice(fd, &mut buf)?; + Ok(buf) +} + +/// Do a single read from `fd` and return the part of the buffer that was written into, +/// and the rest. +#[track_caller] pub fn read_into_slice( fd: libc::c_int, buf: &mut [u8],