diff --git a/library/std/src/sys/pal/common/small_c_string.rs b/library/std/src/sys/pal/common/small_c_string.rs index af9b18e372d..2312e6e5ee2 100644 --- a/library/std/src/sys/pal/common/small_c_string.rs +++ b/library/std/src/sys/pal/common/small_c_string.rs @@ -17,20 +17,27 @@ const NUL_ERR: io::Error = #[inline] pub fn run_path_with_cstr(path: &Path, f: F) -> io::Result where - F: FnOnce(&CStr) -> io::Result, + F: FnMut(&CStr) -> io::Result, { run_with_cstr(path.as_os_str().as_encoded_bytes(), f) } #[inline] -pub fn run_with_cstr(bytes: &[u8], f: F) -> io::Result +pub fn run_with_cstr(bytes: &[u8], mut f: F) -> io::Result where - F: FnOnce(&CStr) -> io::Result, + F: FnMut(&CStr) -> io::Result, { if bytes.len() >= MAX_STACK_ALLOCATION { - return run_with_cstr_allocating(bytes, f); + run_with_cstr_allocating(bytes, &mut f) + } else { + unsafe { run_with_cstr_stack(bytes, &mut f) } } +} +unsafe fn run_with_cstr_stack( + bytes: &[u8], + f: &mut dyn FnMut(&CStr) -> io::Result, +) -> io::Result { let mut buf = MaybeUninit::<[u8; MAX_STACK_ALLOCATION]>::uninit(); let buf_ptr = buf.as_mut_ptr() as *mut u8; @@ -47,10 +54,10 @@ where #[cold] #[inline(never)] -fn run_with_cstr_allocating(bytes: &[u8], f: F) -> io::Result -where - F: FnOnce(&CStr) -> io::Result, -{ +fn run_with_cstr_allocating( + bytes: &[u8], + f: &mut dyn FnMut(&CStr) -> io::Result, +) -> io::Result { match CString::new(bytes) { Ok(s) => f(&s), Err(_) => Err(NUL_ERR), diff --git a/src/tools/miri/tests/fail/shims/fs/isolated_file.stderr b/src/tools/miri/tests/fail/shims/fs/isolated_file.stderr index ec670c4a391..4596367488b 100644 --- a/src/tools/miri/tests/fail/shims/fs/isolated_file.stderr +++ b/src/tools/miri/tests/fail/shims/fs/isolated_file.stderr @@ -11,6 +11,7 @@ LL | let fd = cvt_r(|| unsafe { open64(path.as_ptr(), flags, opts.mode a = note: inside `std::sys::pal::PLATFORM::cvt_r::` at RUSTLIB/std/src/sys/pal/PLATFORM/mod.rs:LL:CC = note: inside `std::sys::pal::PLATFORM::fs::File::open_c` at RUSTLIB/std/src/sys/pal/PLATFORM/fs.rs:LL:CC = note: inside closure at RUSTLIB/std/src/sys/pal/PLATFORM/fs.rs:LL:CC + = note: inside `std::sys::pal::PLATFORM::small_c_string::run_with_cstr_stack::` at RUSTLIB/std/src/sys/pal/PLATFORM/small_c_string.rs:LL:CC = note: inside `std::sys::pal::PLATFORM::small_c_string::run_with_cstr::` at RUSTLIB/std/src/sys/pal/PLATFORM/small_c_string.rs:LL:CC = note: inside `std::sys::pal::PLATFORM::small_c_string::run_path_with_cstr::` at RUSTLIB/std/src/sys/pal/PLATFORM/small_c_string.rs:LL:CC = note: inside `std::sys::pal::PLATFORM::fs::File::open` at RUSTLIB/std/src/sys/pal/PLATFORM/fs.rs:LL:CC