Skip to content

Commit b59e494

Browse files
committed
Fixes #982
1 parent 54905ee commit b59e494

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-9
lines changed

.github/workflows/test.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,11 @@ jobs:
4747
- uses: actions/checkout@v4
4848
- name: Run Tests nightly
4949
run: ./ci.sh test nightly
50+
51+
test-build-16bit:
52+
runs-on: ubuntu-22.04
53+
continue-on-error: true
54+
steps:
55+
- uses: actions/checkout@v4
56+
- name: Build for target with 16 bit pointer
57+
run: ./ci.sh build_16bit

ci.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,16 @@ clippy() {
8282
cargo +$MSRV clippy --tests --examples -- -D warnings
8383
}
8484

85+
build_16bit() {
86+
rustup toolchain install nightly
87+
rustup +nightly component add rust-src
88+
89+
TARGET_WITH_16BIT_POINTER=msp430-none-elf
90+
for features in ${FEATURES_CHECK[@]}; do
91+
cargo +nightly build -Z build-std=core,alloc --target $TARGET_WITH_16BIT_POINTER --no-default-features --features=$features
92+
done
93+
}
94+
8595
coverage() {
8696
for features in ${FEATURES_TEST[@]}; do
8797
cargo llvm-cov --no-report --no-default-features --features "$features"
@@ -121,6 +131,10 @@ if [[ $1 == "clippy" || $1 == "all" ]]; then
121131
clippy
122132
fi
123133

134+
if [[ $1 == "build_16bit" || $1 == "all" ]]; then
135+
build_16bit
136+
fi
137+
124138
if [[ $1 == "coverage" || $1 == "all" ]]; then
125139
coverage
126140
fi

src/socket/tcp.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use core::fmt::Display;
66
#[cfg(feature = "async")]
77
use core::task::Waker;
8-
use core::{cmp, fmt, mem};
8+
use core::{fmt, mem};
99

1010
#[cfg(feature = "async")]
1111
use crate::socket::WakerRegistration;
@@ -510,6 +510,7 @@ impl<'a> Socket<'a> {
510510
// [...] the above constraints imply that 2 * the max window size must be less
511511
// than 2**31 [...] Thus, the shift count must be limited to 14 (which allows
512512
// windows of 2**30 = 1 Gbyte).
513+
#[cfg(not(target_pointer_width = "16"))] // Prevent overflow
513514
if rx_capacity > (1 << 30) {
514515
panic!("receiving buffer too large, cannot exceed 1 GiB")
515516
}
@@ -676,10 +677,7 @@ impl<'a> Socket<'a> {
676677
/// Used in internal calculations as well as packet generation.
677678
#[inline]
678679
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)
683681
}
684682

685683
/// Return the last window field value, including scaling according to RFC 1323.
@@ -698,7 +696,7 @@ impl<'a> Socket<'a> {
698696
let last_win = (self.remote_last_win as usize) << self.remote_win_shift;
699697
let last_win_adjusted = last_ack + last_win - next_ack;
700698

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))
702700
}
703701

704702
/// Set the timeout duration.
@@ -2335,7 +2333,7 @@ impl<'a> Socket<'a> {
23352333
State::SynSent | State::SynReceived => {
23362334
repr.control = TcpControl::Syn;
23372335
// 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);
23392337
if self.state == State::SynSent {
23402338
repr.ack_number = None;
23412339
repr.window_scale = Some(self.remote_win_shift);
@@ -3075,7 +3073,7 @@ mod test {
30753073
ack_number: Some(REMOTE_SEQ + 1),
30763074
max_seg_size: Some(BASE_MSS),
30773075
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),
30793077
..RECV_TEMPL
30803078
}]
30813079
);
@@ -3810,7 +3808,7 @@ mod test {
38103808
ack_number: None,
38113809
max_seg_size: Some(BASE_MSS),
38123810
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),
38143812
sack_permitted: true,
38153813
..RECV_TEMPL
38163814
}]

0 commit comments

Comments
 (0)