5
5
use core:: fmt:: Display ;
6
6
#[ cfg( feature = "async" ) ]
7
7
use core:: task:: Waker ;
8
- use core:: { cmp , fmt, mem} ;
8
+ use core:: { fmt, mem} ;
9
9
10
10
#[ cfg( feature = "async" ) ]
11
11
use crate :: socket:: WakerRegistration ;
@@ -510,6 +510,7 @@ impl<'a> Socket<'a> {
510
510
// [...] the above constraints imply that 2 * the max window size must be less
511
511
// than 2**31 [...] Thus, the shift count must be limited to 14 (which allows
512
512
// windows of 2**30 = 1 Gbyte).
513
+ #[ cfg( not( target_pointer_width = "16" ) ) ] // Prevent overflow
513
514
if rx_capacity > ( 1 << 30 ) {
514
515
panic ! ( "receiving buffer too large, cannot exceed 1 GiB" )
515
516
}
@@ -676,10 +677,7 @@ impl<'a> Socket<'a> {
676
677
/// Used in internal calculations as well as packet generation.
677
678
#[ inline]
678
679
fn scaled_window ( & self ) -> u16 {
679
- cmp:: min (
680
- self . rx_buffer . window ( ) >> self . remote_win_shift as usize ,
681
- ( 1 << 16 ) - 1 ,
682
- ) as u16
680
+ u16:: try_from ( self . rx_buffer . window ( ) >> self . remote_win_shift ) . unwrap_or ( u16:: MAX )
683
681
}
684
682
685
683
/// Return the last window field value, including scaling according to RFC 1323.
@@ -698,7 +696,7 @@ impl<'a> Socket<'a> {
698
696
let last_win = ( self . remote_last_win as usize ) << self . remote_win_shift ;
699
697
let last_win_adjusted = last_ack + last_win - next_ack;
700
698
701
- Some ( cmp :: min ( last_win_adjusted >> self . remote_win_shift , ( 1 << 16 ) - 1 ) as u16 )
699
+ Some ( u16 :: try_from ( last_win_adjusted >> self . remote_win_shift ) . unwrap_or ( u16:: MAX ) )
702
700
}
703
701
704
702
/// Set the timeout duration.
@@ -2335,7 +2333,7 @@ impl<'a> Socket<'a> {
2335
2333
State :: SynSent | State :: SynReceived => {
2336
2334
repr. control = TcpControl :: Syn ;
2337
2335
// window len must NOT be scaled in SYNs.
2338
- repr. window_len = self . rx_buffer . window ( ) . min ( ( 1 << 16 ) - 1 ) as u16 ;
2336
+ repr. window_len = u16 :: try_from ( self . rx_buffer . window ( ) ) . unwrap_or ( u16:: MAX ) ;
2339
2337
if self . state == State :: SynSent {
2340
2338
repr. ack_number = None ;
2341
2339
repr. window_scale = Some ( self . remote_win_shift ) ;
@@ -3075,7 +3073,7 @@ mod test {
3075
3073
ack_number: Some ( REMOTE_SEQ + 1 ) ,
3076
3074
max_seg_size: Some ( BASE_MSS ) ,
3077
3075
window_scale: Some ( * shift_amt) ,
3078
- window_len: cmp :: min ( * buffer_size, 65535 ) as u16 ,
3076
+ window_len: u16 :: try_from ( * buffer_size) . unwrap_or ( u16 :: MAX ) ,
3079
3077
..RECV_TEMPL
3080
3078
} ]
3081
3079
) ;
@@ -3810,7 +3808,7 @@ mod test {
3810
3808
ack_number: None ,
3811
3809
max_seg_size: Some ( BASE_MSS ) ,
3812
3810
window_scale: Some ( * shift_amt) ,
3813
- window_len: cmp :: min ( * buffer_size, 65535 ) as u16 ,
3811
+ window_len: u16 :: try_from ( * buffer_size) . unwrap_or ( u16 :: MAX ) ,
3814
3812
sack_permitted: true ,
3815
3813
..RECV_TEMPL
3816
3814
} ]
0 commit comments