11#![ unstable( issue = "none" , feature = "windows_handle" ) ]
2- #![ allow( unsafe_op_in_unsafe_fn) ]
32
43#[ cfg( test) ]
54mod tests;
@@ -73,7 +72,7 @@ impl IntoRawHandle for Handle {
7372
7473impl FromRawHandle for Handle {
7574 unsafe fn from_raw_handle ( raw_handle : RawHandle ) -> Self {
76- Self ( FromRawHandle :: from_raw_handle ( raw_handle) )
75+ unsafe { Self ( FromRawHandle :: from_raw_handle ( raw_handle) ) }
7776 }
7877}
7978
@@ -139,13 +138,23 @@ impl Handle {
139138
140139 pub unsafe fn read_overlapped (
141140 & self ,
142- buf : & mut [ u8 ] ,
141+ buf : & mut [ mem :: MaybeUninit < u8 > ] ,
143142 overlapped : * mut c:: OVERLAPPED ,
144143 ) -> io:: Result < Option < usize > > {
145- let len = cmp:: min ( buf. len ( ) , u32:: MAX as usize ) as u32 ;
146- let mut amt = 0 ;
147- let res =
148- cvt ( c:: ReadFile ( self . as_raw_handle ( ) , buf. as_mut_ptr ( ) , len, & mut amt, overlapped) ) ;
144+ // SAFETY: We have exclusive access to the buffer and it's up to the caller to
145+ // ensure the OVERLAPPED pointer is valid for the lifetime of this function.
146+ let ( res, amt) = unsafe {
147+ let len = cmp:: min ( buf. len ( ) , u32:: MAX as usize ) as u32 ;
148+ let mut amt = 0 ;
149+ let res = cvt ( c:: ReadFile (
150+ self . as_raw_handle ( ) ,
151+ buf. as_mut_ptr ( ) . cast :: < u8 > ( ) ,
152+ len,
153+ & mut amt,
154+ overlapped,
155+ ) ) ;
156+ ( res, amt)
157+ } ;
149158 match res {
150159 Ok ( _) => Ok ( Some ( amt as usize ) ) ,
151160 Err ( e) => {
@@ -230,20 +239,24 @@ impl Handle {
230239
231240 // The length is clamped at u32::MAX.
232241 let len = cmp:: min ( len, u32:: MAX as usize ) as u32 ;
233- let status = c:: NtReadFile (
234- self . as_handle ( ) ,
235- ptr:: null_mut ( ) ,
236- None ,
237- ptr:: null_mut ( ) ,
238- & mut io_status,
239- buf,
240- len,
241- offset. map ( |n| n as _ ) . as_ref ( ) ,
242- None ,
243- ) ;
242+ // SAFETY: It's up to the caller to ensure `buf` is writeable up to
243+ // the provided `len`.
244+ let status = unsafe {
245+ c:: NtReadFile (
246+ self . as_handle ( ) ,
247+ ptr:: null_mut ( ) ,
248+ None ,
249+ ptr:: null_mut ( ) ,
250+ & mut io_status,
251+ buf,
252+ len,
253+ offset. map ( |n| n as _ ) . as_ref ( ) ,
254+ None ,
255+ )
256+ } ;
244257
245258 let status = if status == c:: STATUS_PENDING {
246- c:: WaitForSingleObject ( self . as_raw_handle ( ) , c:: INFINITE ) ;
259+ unsafe { c:: WaitForSingleObject ( self . as_raw_handle ( ) , c:: INFINITE ) } ;
247260 io_status. status ( )
248261 } else {
249262 status
@@ -261,7 +274,7 @@ impl Handle {
261274 status if c:: nt_success ( status) => Ok ( io_status. Information ) ,
262275
263276 status => {
264- let error = c:: RtlNtStatusToDosError ( status) ;
277+ let error = unsafe { c:: RtlNtStatusToDosError ( status) } ;
265278 Err ( io:: Error :: from_raw_os_error ( error as _ ) )
266279 }
267280 }
0 commit comments