Skip to content

Commit 88c05ed

Browse files
committed
Make synchronous_write safe to call
1 parent 084b71a commit 88c05ed

File tree

2 files changed

+18
-23
lines changed

2 files changed

+18
-23
lines changed

library/std/src/sys/windows/c.rs

-7
Original file line numberDiff line numberDiff line change
@@ -986,13 +986,6 @@ extern "system" {
986986
lpOverlapped: LPOVERLAPPED,
987987
lpCompletionRoutine: LPOVERLAPPED_COMPLETION_ROUTINE,
988988
) -> BOOL;
989-
pub fn WriteFile(
990-
hFile: BorrowedHandle<'_>,
991-
lpBuffer: LPVOID,
992-
nNumberOfBytesToWrite: DWORD,
993-
lpNumberOfBytesWritten: LPDWORD,
994-
lpOverlapped: LPOVERLAPPED,
995-
) -> BOOL;
996989
pub fn WriteFileEx(
997990
hFile: BorrowedHandle<'_>,
998991
lpBuffer: LPVOID,

library/std/src/sys/windows/handle.rs

+18-16
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ impl Handle {
192192
}
193193

194194
pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
195-
unsafe { self.synchronous_write(&buf, None) }
195+
self.synchronous_write(&buf, None)
196196
}
197197

198198
pub fn write_vectored(&self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
@@ -205,7 +205,7 @@ impl Handle {
205205
}
206206

207207
pub fn write_at(&self, buf: &[u8], offset: u64) -> io::Result<usize> {
208-
unsafe { self.synchronous_write(&buf, Some(offset)) }
208+
self.synchronous_write(&buf, Some(offset))
209209
}
210210

211211
pub fn try_clone(&self) -> io::Result<Self> {
@@ -276,25 +276,27 @@ impl Handle {
276276
/// See #81357.
277277
///
278278
/// If `offset` is `None` then the current file position is used.
279-
unsafe fn synchronous_write(&self, buf: &[u8], offset: Option<u64>) -> io::Result<usize> {
279+
fn synchronous_write(&self, buf: &[u8], offset: Option<u64>) -> io::Result<usize> {
280280
let mut io_status = c::IO_STATUS_BLOCK::default();
281281

282282
// The length is clamped at u32::MAX.
283283
let len = cmp::min(buf.len(), c::DWORD::MAX as usize) as c::DWORD;
284-
let status = c::NtWriteFile(
285-
self.as_handle(),
286-
ptr::null_mut(),
287-
None,
288-
ptr::null_mut(),
289-
&mut io_status,
290-
buf.as_ptr(),
291-
len,
292-
offset.map(|n| n as _).as_ref(),
293-
None,
294-
);
284+
let status = unsafe {
285+
c::NtWriteFile(
286+
self.as_handle(),
287+
ptr::null_mut(),
288+
None,
289+
ptr::null_mut(),
290+
&mut io_status,
291+
buf.as_ptr(),
292+
len,
293+
offset.map(|n| n as _).as_ref(),
294+
None,
295+
)
296+
};
295297
match status {
296298
// If the operation has not completed then abort the process.
297-
// Doing otherwise means that the buffer maybe read and the stack
299+
// Doing otherwise means that the buffer may be read and the stack
298300
// written to after this function returns.
299301
c::STATUS_PENDING => {
300302
eprintln!("I/O error: operation failed to complete synchronously");
@@ -305,7 +307,7 @@ impl Handle {
305307
status if c::nt_success(status) => Ok(io_status.Information),
306308

307309
status => {
308-
let error = c::RtlNtStatusToDosError(status);
310+
let error = unsafe { c::RtlNtStatusToDosError(status) };
309311
Err(io::Error::from_raw_os_error(error as _))
310312
}
311313
}

0 commit comments

Comments
 (0)