@@ -321,11 +321,15 @@ mod tests {
321321 fn test_ip_addr_conversion ( ) {
322322 let core_addr = core:: net:: IpAddr :: V4 ( core:: net:: Ipv4Addr :: from ( TEST_IPV4 ) ) ;
323323 let uefi_addr = IpAddress :: from ( core_addr) ;
324- assert_eq ! ( unsafe { uefi_addr. v4. 0 } , TEST_IPV4 ) ;
324+ // SAFETY: `IpAddress::from` constructed the IPv4 variant above.
325+ let octets = unsafe { uefi_addr. v4 . 0 } ;
326+ assert_eq ! ( octets, TEST_IPV4 ) ;
325327
326328 let core_addr = core:: net:: IpAddr :: V6 ( core:: net:: Ipv6Addr :: from ( TEST_IPV6 ) ) ;
327329 let uefi_addr = IpAddress :: from ( core_addr) ;
328- assert_eq ! ( unsafe { uefi_addr. v6. 0 } , TEST_IPV6 ) ;
330+ // SAFETY: `IpAddress::from` constructed the IPv6 variant above.
331+ let octets = unsafe { uefi_addr. v6 . 0 } ;
332+ assert_eq ! ( octets, TEST_IPV6 ) ;
329333 }
330334
331335 /// Test conversions between `MacAddress` and octet arrays.
@@ -373,40 +377,42 @@ mod tests {
373377 let octets = [ 0_u8 , 1 , 2 , 3 ] ;
374378 assert_eq ! ( Ipv4Address :: from( octets) , Ipv4Address ( octets) ) ;
375379 let uefi_addr = IpAddress :: from ( octets) ;
380+ // SAFETY: `from([u8; 4])` constructs the IPv4 variant.
376381 assert_eq ! ( & octets, & unsafe { uefi_addr. v4. octets( ) } ) ;
377382 }
378383 // octets -> Ipv6Address
379384 {
380385 let octets = [ 0_u8 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 ] ;
381386 assert_eq ! ( Ipv6Address :: from( octets) , Ipv6Address ( octets) ) ;
382387 let uefi_addr = IpAddress :: from ( octets) ;
388+ // SAFETY: `from([u8; 16])` constructs the IPv6 variant.
383389 assert_eq ! ( & octets, & unsafe { uefi_addr. v6. octets( ) } ) ;
384390 }
385391 // StdIpv4Addr -> Ipv4Address
386392 {
387393 let octets = [ 7 , 5 , 3 , 1 ] ;
388394 let core_ipv4_addr = core:: net:: Ipv4Addr :: from ( octets) ;
389395 assert_eq ! ( Ipv4Address :: from( core_ipv4_addr) . octets( ) , octets) ;
390- assert_eq ! (
391- unsafe { IpAddress :: from( core_ipv4_addr) . v4. octets( ) } ,
392- octets
393- ) ;
396+ // SAFETY: The `Ipv4Addr` conversion selects the IPv4 variant.
397+ let converted = unsafe { IpAddress :: from ( core_ipv4_addr) . v4 . octets ( ) } ;
398+ assert_eq ! ( converted, octets) ;
394399 }
395400 // StdIpv6Addr -> Ipv6Address
396401 {
397402 let octets = [ 7 , 5 , 3 , 1 , 6 , 3 , 8 , 5 , 2 , 5 , 2 , 7 , 3 , 5 , 2 , 6 ] ;
398403 let core_ipv6_addr = core:: net:: Ipv6Addr :: from ( octets) ;
399404 assert_eq ! ( Ipv6Address :: from( core_ipv6_addr) . octets( ) , octets) ;
400- assert_eq ! (
401- unsafe { IpAddress :: from( core_ipv6_addr) . v6. octets( ) } ,
402- octets
403- ) ;
405+ // SAFETY: The `Ipv6Addr` conversion selects the IPv6 variant.
406+ let converted = unsafe { IpAddress :: from ( core_ipv6_addr) . v6 . octets ( ) } ;
407+ assert_eq ! ( converted, octets) ;
404408 }
405409 // StdIpAddr -> IpAddress
406410 {
407411 let octets = [ 8 , 8 , 2 , 6 ] ;
408412 let core_ip_addr = core:: net:: IpAddr :: from ( octets) ;
409- assert_eq ! ( unsafe { IpAddress :: from( core_ip_addr) . v4. octets( ) } , octets) ;
413+ // SAFETY: This `IpAddr` is built from four octets, so it is IPv4.
414+ let converted = unsafe { IpAddress :: from ( core_ip_addr) . v4 . octets ( ) } ;
415+ assert_eq ! ( converted, octets) ;
410416 }
411417 // octets <-> MacAddress
412418 {
@@ -435,15 +441,17 @@ mod tests {
435441 #[ test]
436442 fn test_uefi_flow ( ) {
437443 fn efi_retrieve_efi_ip_addr ( addr : * mut IpAddress , is_ipv6 : bool ) {
444+ // SAFETY: The caller passes a valid, writable `IpAddress` pointer.
438445 let addr = unsafe { addr. as_mut ( ) . unwrap ( ) } ;
439- // SAFETY: Alignment is guaranteed and memory is initialized.
446+ // SAFETY: The selected variant is the one initialized by the caller .
440447 unsafe {
441448 addr. v4 . 0 [ 0 ] = 42 ;
442449 addr. v4 . 0 [ 1 ] = 42 ;
443450 addr. v4 . 0 [ 2 ] = 42 ;
444451 addr. v4 . 0 [ 3 ] = 42 ;
445452 }
446453 if is_ipv6 {
454+ // SAFETY: The IPv6 branch writes the bytes that the caller will read back.
447455 unsafe {
448456 addr. v6 . 0 [ 14 ] = 42 ;
449457 addr. v6 . 0 [ 15 ] = 42 ;
@@ -454,6 +462,7 @@ mod tests {
454462 fn high_level_retrieve_ip ( is_ipv6 : bool ) -> core:: net:: IpAddr {
455463 let mut efi_ip_addr = IpAddress :: ZERO ;
456464 efi_retrieve_efi_ip_addr ( & mut efi_ip_addr, is_ipv6) ;
465+ // SAFETY: The helper initialized the variant selected by `is_ipv6`.
457466 unsafe { efi_ip_addr. into_core_addr ( is_ipv6) }
458467 }
459468
0 commit comments