From 7d90a0eb0438b7258901b22813abf792172879f7 Mon Sep 17 00:00:00 2001 From: Sebastien Tardif Date: Wed, 24 Jun 2026 07:03:25 -0700 Subject: [PATCH] std: reject Windows File::truncate sizes that exceed i64::MAX size as i64 silently wraps huge values to negative EOF offsets. Match Unix ftruncate and return InvalidInput instead. Signed-off-by: Sebastien Tardif --- library/std/src/sys/fs/windows.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/library/std/src/sys/fs/windows.rs b/library/std/src/sys/fs/windows.rs index e3e7b081b47d5..829d68f1c89c4 100644 --- a/library/std/src/sys/fs/windows.rs +++ b/library/std/src/sys/fs/windows.rs @@ -515,7 +515,11 @@ impl File { } pub fn truncate(&self, size: u64) -> io::Result<()> { - let info = c::FILE_END_OF_FILE_INFO { EndOfFile: size as i64 }; + // Windows expects a signed 64-bit EOF; reject sizes that would wrap + // (matches Unix `ftruncate` which uses `try_into` for `off_t`). + let size: i64 = + size.try_into().map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e))?; + let info = c::FILE_END_OF_FILE_INFO { EndOfFile: size }; api::set_file_information_by_handle(self.handle.as_raw_handle(), &info).io_result() }