@@ -125,12 +125,17 @@ Exit the app anytime by pressing Ctrl-C
125125 ) ;
126126 }
127127
128- // SAFETY:
129- // Call Win32 API FFI GetLastError() to check for any errors
130- unsafe {
131- if h_device == INVALID_HANDLE_VALUE {
132- return Err ( format ! ( "Failed to open device. Error {}" , GetLastError ( ) ) . into ( ) ) ;
133- }
128+ if h_device == INVALID_HANDLE_VALUE {
129+ return Err ( format ! (
130+ "Failed to open device. Error {}" ,
131+ // SAFETY:
132+ // - FFI contract: Called immediately after CreateFileW failure before any intervening
133+ // Win32/CRT calls that would overwrite thread-local error slot
134+ // - Concurrency: Reads thread-local storage only (no data races)
135+ // - Memory safety: Returns u32 value (no pointer dereferences)
136+ unsafe { GetLastError ( ) }
137+ )
138+ . into ( ) ) ;
134139 }
135140
136141 println ! ( "Opened device successfully" ) ;
@@ -204,16 +209,17 @@ fn perform_write_read_test(h_device: HANDLE, test_length: u32) -> Result<(), Box
204209 ) ;
205210 }
206211
207- // SAFETY:
208- // Call Win32 API FFI GetLastError() to check for any errors from WriteFile
209- unsafe {
210- if r == FALSE {
211- return Err ( format ! (
212- "PerformWriteReadTest: WriteFile failed: Error {}" ,
213- GetLastError ( )
214- )
215- . into ( ) ) ;
216- }
212+ if r == FALSE {
213+ return Err ( format ! (
214+ "PerformWriteReadTest: WriteFile failed: Error {}" ,
215+ // SAFETY:
216+ // - FFI contract: Called immediately after WriteFile failure before any intervening
217+ // Win32/CRT calls that would overwrite thread-local error slot
218+ // - Concurrency: Reads thread-local storage only (no data races)
219+ // - Memory safety: Returns u32 value (no pointer dereferences)
220+ unsafe { GetLastError ( ) }
221+ )
222+ . into ( ) ) ;
217223 }
218224
219225 if bytes_returned != test_length {
@@ -239,16 +245,17 @@ fn perform_write_read_test(h_device: HANDLE, test_length: u32) -> Result<(), Box
239245 ) ;
240246 }
241247
242- // SAFETY:
243- // Call Win32 API FFI GetLastError() to check for any errors from ReadFile
244- unsafe {
245- if r == FALSE {
246- return Err ( format ! (
247- "PerformWriteReadTest: ReadFile failed: Error {}" ,
248- GetLastError ( )
249- )
250- . into ( ) ) ;
251- }
248+ if r == FALSE {
249+ return Err ( format ! (
250+ "PerformWriteReadTest: ReadFile failed: Error {}" ,
251+ // SAFETY:
252+ // - FFI contract: Called immediately after ReadFile failure before any intervening
253+ // Win32/CRT calls that would overwrite thread-local error slot
254+ // - Concurrency: Reads thread-local storage only (no data races)
255+ // - Memory safety: Returns u32 value (no pointer dereferences)
256+ unsafe { GetLastError ( ) }
257+ )
258+ . into ( ) ) ;
252259 }
253260
254261 // SAFETY:
@@ -309,17 +316,18 @@ fn async_io_work(io_type: u32) -> Result<(), Box<dyn Error>> {
309316 ) ;
310317 }
311318
312- // SAFETY:
313- // Call Win32 API FFI GetLastError() to check for any errors from CreateFileW
314- unsafe {
315- if h_device == INVALID_HANDLE_VALUE {
316- return Err ( format ! (
317- "Cannot open {} error {}" ,
318- globals. device_path,
319- GetLastError ( )
320- )
321- . into ( ) ) ;
322- }
319+ if h_device == INVALID_HANDLE_VALUE {
320+ return Err ( format ! (
321+ "Cannot open {} error {}" ,
322+ globals. device_path,
323+ // SAFETY:
324+ // - FFI contract: Called immediately after CreateFileW failure before any intervening
325+ // Win32/CRT calls that would overwrite thread-local error slot
326+ // - Concurrency: Reads thread-local storage only (no data races)
327+ // - Memory safety: Returns u32 value (no pointer dereferences)
328+ unsafe { GetLastError ( ) }
329+ )
330+ . into ( ) ) ;
323331 }
324332
325333 // SAFETY:
@@ -329,14 +337,18 @@ fn async_io_work(io_type: u32) -> Result<(), Box<dyn Error>> {
329337 h_completion_port = CreateIoCompletionPort ( h_device, 0 , 1 , 0 ) ;
330338 }
331339
332- // SAFETY:
333- // Call Win32 API FFI to check for CreateIoCompletionPort result from
334- // GetLastError()
335- unsafe {
336- // CreateIoCompletionPort returns NULL on failure, not INVALID_HANDLE_VALUE
337- if h_completion_port == 0 {
338- return Err ( format ! ( "Cannot open completion port {}" , GetLastError ( ) ) . into ( ) ) ;
339- }
340+ // CreateIoCompletionPort returns NULL on failure, not INVALID_HANDLE_VALUE
341+ if h_completion_port == 0 {
342+ return Err ( format ! (
343+ "Cannot open completion port {}" ,
344+ // SAFETY:
345+ // - FFI contract: Called immediately after CreateIoCompletionPort failure before any
346+ // intervening Win32/CRT calls that would overwrite thread-local error slot
347+ // - Concurrency: Reads thread-local storage only (no data races)
348+ // - Memory safety: Returns u32 value (no pointer dereferences)
349+ unsafe { GetLastError ( ) }
350+ )
351+ . into ( ) ) ;
340352 }
341353
342354 let mut remaining_requests_to_receive = 0 ;
@@ -394,14 +406,15 @@ fn async_io_work(io_type: u32) -> Result<(), Box<dyn Error>> {
394406 ) ;
395407 }
396408
397- // SAFETY:
398- // Call Win32 API FFI GetLastError() to check for any errors from ReadFile
399- unsafe {
400- if r == FALSE {
401- let error = GetLastError ( ) ;
402- if error != ERROR_IO_PENDING {
403- return Err ( format ! ( "{i}th Read failed {error}" ) . into ( ) ) ;
404- }
409+ if r == FALSE {
410+ // SAFETY:
411+ // - FFI contract: Called immediately after ReadFile failure before any
412+ // intervening Win32/CRT calls that would overwrite thread-local error slot
413+ // - Concurrency: Reads thread-local storage only (no data races)
414+ // - Memory safety: Returns u32 value (no pointer dereferences)
415+ let error = unsafe { GetLastError ( ) } ;
416+ if error != ERROR_IO_PENDING {
417+ return Err ( format ! ( "{i}th Read failed {error}" , ) . into ( ) ) ;
405418 }
406419 }
407420 } else {
@@ -419,14 +432,15 @@ fn async_io_work(io_type: u32) -> Result<(), Box<dyn Error>> {
419432 ) ;
420433 }
421434
422- // SAFETY:
423- // Call Win32 API FFI GetLastError() to check for any errors from WriteFile
424- unsafe {
425- if r == FALSE {
426- let error = GetLastError ( ) ;
427- if error != ERROR_IO_PENDING {
428- return Err ( format ! ( "{i}th Write failed {error}" ) . into ( ) ) ;
429- }
435+ if r == FALSE {
436+ // SAFETY:
437+ // - FFI contract: Called immediately after WriteFile failure before any
438+ // intervening Win32/CRT calls that would overwrite thread-local error slot
439+ // - Concurrency: Reads thread-local storage only (no data races)
440+ // - Memory safety: Returns u32 value (no pointer dereferences)
441+ let error = unsafe { GetLastError ( ) } ;
442+ if error != ERROR_IO_PENDING {
443+ return Err ( format ! ( "{i}th Write failed {error}" ) . into ( ) ) ;
430444 }
431445 }
432446 }
@@ -450,13 +464,18 @@ fn async_io_work(io_type: u32) -> Result<(), Box<dyn Error>> {
450464 ) ;
451465 }
452466
453- // SAFETY:
454- // Call Win32 API FFI GetLastError() to check for any errors from
455- // GetQueuedCompletionStatus
456- unsafe {
457- if r == FALSE {
458- return Err ( format ! ( "GetQueuedCompletionStatus failed {}" , GetLastError ( ) ) . into ( ) ) ;
459- }
467+ if r == FALSE {
468+ return Err ( format ! (
469+ "GetQueuedCompletionStatus failed {}" ,
470+ // SAFETY:
471+ // - FFI contract: Called immediately after GetQueuedCompletionStatus failure
472+ // before any intervening Win32/CRT calls that would overwrite thread-local error
473+ // slot
474+ // - Concurrency: Reads thread-local storage only (no data races)
475+ // - Memory safety: Returns u32 value (no pointer dereferences)
476+ unsafe { GetLastError ( ) }
477+ )
478+ . into ( ) ) ;
460479 }
461480
462481 let i;
@@ -508,14 +527,15 @@ fn async_io_work(io_type: u32) -> Result<(), Box<dyn Error>> {
508527 ) ;
509528 }
510529
511- // SAFETY:
512- // Call Win32 API FFI GetLastError() to check for any errors from ReadFile
513- unsafe {
514- if r == FALSE {
515- let error = GetLastError ( ) ;
516- if error != ERROR_IO_PENDING {
517- return Err ( format ! ( "{i}th Read failed {error}" ) . into ( ) ) ;
518- }
530+ if r == FALSE {
531+ // SAFETY:
532+ // - FFI contract: Called immediately after ReadFile failure before any
533+ // intervening Win32/CRT calls that would overwrite thread-local error slot
534+ // - Concurrency: Reads thread-local storage only (no data races)
535+ // - Memory safety: Returns u32 value (no pointer dereferences)
536+ let error = unsafe { GetLastError ( ) } ;
537+ if error != ERROR_IO_PENDING {
538+ return Err ( format ! ( "{i}th Read failed {error}" ) . into ( ) ) ;
519539 }
520540 }
521541 } else {
@@ -559,14 +579,15 @@ fn async_io_work(io_type: u32) -> Result<(), Box<dyn Error>> {
559579 ) ;
560580 }
561581
562- // SAFETY:
563- // Call Win32 API FFI GetLastError() to check for any errors from WriteFile
564- unsafe {
565- if r == FALSE {
566- let error = GetLastError ( ) ;
567- if error != ERROR_IO_PENDING {
568- return Err ( format ! ( "{i}th write failed {error}" ) . into ( ) ) ;
569- }
582+ if r == FALSE {
583+ // SAFETY:
584+ // - FFI contract: Called immediately after WriteFile failure before any
585+ // intervening Win32/CRT calls that would overwrite thread-local error slot
586+ // - Concurrency: Reads thread-local storage only (no data races)
587+ // - Memory safety: Returns u32 value (no pointer dereferences)
588+ let error = unsafe { GetLastError ( ) } ;
589+ if error != ERROR_IO_PENDING {
590+ return Err ( format ! ( "{i}th write failed {error}" ) . into ( ) ) ;
570591 }
571592 }
572593 }
0 commit comments