补充 Windows 原子替换安全说明

记录 MoveFileExW 路径缓冲区的所有权、终止符和调用生命周期不变量。
This commit is contained in:
2026-09-12 21:27:57 +08:00
parent 8135b4c374
commit b90d12c5fb
@@ -141,7 +141,7 @@ fn replace_separation_state_atomically(temporary: &Path, target: &Path) -> std::
fn replace_separation_state_atomically(temporary: &Path, target: &Path) -> std::io::Result<()> {
use std::os::windows::ffi::OsStrExt;
use windows_sys::Win32::Storage::FileSystem::{
MOVEFILE_REPLACE_EXISTING, MOVEFILE_WRITE_THROUGH, MoveFileExW,
MoveFileExW, MOVEFILE_REPLACE_EXISTING, MOVEFILE_WRITE_THROUGH,
};
let source = temporary
@@ -154,6 +154,8 @@ fn replace_separation_state_atomically(temporary: &Path, target: &Path) -> std::
.encode_wide()
.chain(std::iter::once(0))
.collect::<Vec<_>>();
// SAFETY: both buffers are owned, UTF-16 encoded, and NUL-terminated; they
// remain alive for the duration of the call, which only reads the paths.
let moved = unsafe {
MoveFileExW(
source.as_ptr(),