@@ -605,11 +605,14 @@ impl fmt::Display for SocketAddrV4 {
605605 if f. precision ( ) . is_none ( ) && f. width ( ) . is_none ( ) {
606606 write ! ( f, "{}:{}" , self . ip( ) , self . port( ) )
607607 } else {
608- const IPV4_SOCKET_BUF_LEN : usize = 21 ;
608+ const IPV4_SOCKET_BUF_LEN : usize = ( 3 * 4 ) // the segments
609+ + 3 // the separators
610+ + 1 + 5 ; // the port
609611 let mut buf = [ 0 ; IPV4_SOCKET_BUF_LEN ] ;
610612 let mut buf_slice = & mut buf[ ..] ;
611613
612- // Unwrap is fine because writing to a buffer is infallible
614+ // Unwrap is fine because writing to a sufficiently-sized
615+ // buffer is infallible
613616 write ! ( buf_slice, "{}:{}" , self . ip( ) , self . port( ) ) . unwrap ( ) ;
614617 let len = IPV4_SOCKET_BUF_LEN - buf_slice. len ( ) ;
615618
@@ -643,7 +646,8 @@ impl fmt::Display for SocketAddrV6 {
643646 let mut buf = [ 0 ; IPV6_SOCKET_BUF_LEN ] ;
644647 let mut buf_slice = & mut buf[ ..] ;
645648
646- // Unwrap is fine because writing to a buffer is infallible
649+ // Unwrap is fine because writing to a sufficiently-sized
650+ // buffer is infallible
647651 write ! ( buf_slice, "[{}]:{}" , self . ip( ) , self . port( ) ) . unwrap ( ) ;
648652 let len = IPV6_SOCKET_BUF_LEN - buf_slice. len ( ) ;
649653
0 commit comments