@@ -1462,6 +1462,7 @@ mod tests {
14621462 impl SeqpacketTestStream {
14631463 fn new ( ) -> Self {
14641464 let mut fds = [ 0i32 ; 2 ] ;
1465+ // SAFETY: valid AF_UNIX socketpair call; fds is a valid 2-element array.
14651466 let ret = unsafe {
14661467 libc:: socketpair (
14671468 libc:: AF_UNIX ,
@@ -1479,19 +1480,21 @@ mod tests {
14791480
14801481 // Write one seqpacket message into the remote end.
14811482 fn push_message ( & self , data : & [ u8 ] ) {
1483+ // SAFETY: `remote_fd` is valid; `data` is a valid slice for the duration of the call.
14821484 let ret = unsafe {
14831485 libc:: write (
14841486 self . remote_fd ,
1485- data. as_ptr ( ) as * const libc:: c_void ,
1487+ data. as_ptr ( ) . cast :: < libc:: c_void > ( ) ,
14861488 data. len ( ) ,
14871489 )
14881490 } ;
1489- assert_eq ! ( ret as usize , data. len( ) , "push_message write failed" ) ;
1491+ assert_eq ! ( ret. cast_unsigned ( ) , data. len( ) , "push_message write failed" ) ;
14901492 }
14911493 }
14921494
14931495 impl Drop for SeqpacketTestStream {
14941496 fn drop ( & mut self ) {
1497+ // SAFETY: Both fds are valid and owned by this struct; closing them on drop.
14951498 unsafe {
14961499 libc:: close ( self . local_fd ) ;
14971500 libc:: close ( self . remote_fd ) ;
@@ -1511,36 +1514,40 @@ mod tests {
15111514 buf : & mut VolatileSlice < B > ,
15121515 ) -> Result < usize , VolatileMemoryError > {
15131516 let mut tmp = vec ! [ 0u8 ; buf. len( ) ] ;
1517+ // SAFETY: `local_fd` is valid; `tmp` is a valid writable buffer for the duration of
1518+ // the call.
15141519 let ret = unsafe {
15151520 libc:: recv (
15161521 self . local_fd ,
1517- tmp. as_mut_ptr ( ) as * mut libc:: c_void ,
1522+ tmp. as_mut_ptr ( ) . cast :: < libc:: c_void > ( ) ,
15181523 tmp. len ( ) ,
15191524 0 ,
15201525 )
15211526 } ;
15221527 if ret < 0 {
15231528 return Err ( VolatileMemoryError :: IOError ( IoError :: last_os_error ( ) ) ) ;
15241529 }
1525- let n = ret as usize ;
1530+ let n = ret. cast_unsigned ( ) ;
15261531 buf. copy_from ( & tmp[ ..n] ) ;
15271532 Ok ( n)
15281533 }
15291534 }
15301535
15311536 impl Write for SeqpacketTestStream {
15321537 fn write ( & mut self , data : & [ u8 ] ) -> Result < usize , IoError > {
1538+ // SAFETY: `local_fd` is valid; `data` is a valid readable slice for the duration of
1539+ // the call.
15331540 let ret = unsafe {
15341541 libc:: write (
15351542 self . local_fd ,
1536- data. as_ptr ( ) as * const libc:: c_void ,
1543+ data. as_ptr ( ) . cast :: < libc:: c_void > ( ) ,
15371544 data. len ( ) ,
15381545 )
15391546 } ;
15401547 if ret < 0 {
15411548 Err ( IoError :: last_os_error ( ) )
15421549 } else {
1543- Ok ( ret as usize )
1550+ Ok ( ret. cast_unsigned ( ) )
15441551 }
15451552 }
15461553
@@ -1556,17 +1563,19 @@ mod tests {
15561563 ) -> Result < usize , VolatileMemoryError > {
15571564 let mut tmp = vec ! [ 0u8 ; buf. len( ) ] ;
15581565 buf. copy_to ( & mut tmp) ;
1566+ // SAFETY: `local_fd` is valid; `tmp` is a valid readable buffer for the duration of
1567+ // the call.
15591568 let ret = unsafe {
15601569 libc:: write (
15611570 self . local_fd ,
1562- tmp. as_ptr ( ) as * const libc:: c_void ,
1571+ tmp. as_ptr ( ) . cast :: < libc:: c_void > ( ) ,
15631572 tmp. len ( ) ,
15641573 )
15651574 } ;
15661575 if ret < 0 {
15671576 Err ( VolatileMemoryError :: IOError ( IoError :: last_os_error ( ) ) )
15681577 } else {
1569- Ok ( ret as usize )
1578+ Ok ( ret. cast_unsigned ( ) )
15701579 }
15711580 }
15721581 }
@@ -1657,7 +1666,7 @@ mod tests {
16571666 // First call: fills the descriptor (4096 bytes), does not set EOM.
16581667 let res1 = conn. recv_pkt ( & mut rx_pkt) . unwrap ( ) ;
16591668 assert_eq ! ( rx_pkt. hdr. op( ) , uapi:: VSOCK_OP_RW ) ;
1660- assert_eq ! ( res1. bytes_read, BUF_SIZE as u32 ) ;
1669+ assert_eq ! ( res1. bytes_read, u32 :: try_from ( BUF_SIZE ) . unwrap ( ) ) ;
16611670 assert ! ( res1. should_retrigger) ;
16621671 assert_eq ! ( rx_pkt. hdr. flags( ) & VIRTIO_VSOCK_SEQ_EOM , 0 ) ;
16631672 // Connection must still have pending RX for the remainder.
@@ -1687,7 +1696,7 @@ mod tests {
16871696 let res = conn. recv_pkt ( & mut rx_pkt) . unwrap ( ) ;
16881697
16891698 assert_eq ! ( rx_pkt. hdr. op( ) , uapi:: VSOCK_OP_RW ) ;
1690- assert_eq ! ( res. bytes_read, BUF_SIZE as u32 ) ;
1699+ assert_eq ! ( res. bytes_read, u32 :: try_from ( BUF_SIZE ) . unwrap ( ) ) ;
16911700 assert ! ( !res. should_retrigger) ;
16921701 assert_ne ! ( rx_pkt. hdr. flags( ) & VIRTIO_VSOCK_SEQ_EOM , 0 ) ;
16931702 assert ! ( !conn. has_pending_rx( ) ) ;
@@ -1704,8 +1713,7 @@ mod tests {
17041713
17051714 let stream = SeqpacketTestStream :: new ( ) ;
17061715 stream. push_message ( & vec ! [ 0u8 ; MSG_LEN ] ) ;
1707- let ( mut conn, mut rx_pkt, _ctx) =
1708- make_established_seqpacket ( stream, Some ( SMALL_BUF ) ) ;
1716+ let ( mut conn, mut rx_pkt, _ctx) = make_established_seqpacket ( stream, Some ( SMALL_BUF ) ) ;
17091717
17101718 conn. notify ( EventSet :: IN ) ;
17111719
0 commit comments