Skip to content

Commit dc40eba

Browse files
committed
Review fixes
1 parent 9d07084 commit dc40eba

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

p2p/src/network/yamux/p2p_network_yamux_reducer.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,12 @@ impl P2pNetworkYamuxState {
136136
}
137137

138138
match &frame.inner {
139-
YamuxFrameInner::Data(data) => {
139+
YamuxFrameInner::Data(_) => {
140140
if let Some(stream) = yamux_state.streams.get_mut(&frame.stream_id) {
141141
// must not underflow
142142
// TODO: check it and disconnect peer that violates flow rules
143143
stream.window_ours =
144-
stream.window_ours.saturating_sub(data.len() as u32);
144+
stream.window_ours.saturating_sub(frame.len_as_u32());
145145
}
146146
}
147147
YamuxFrameInner::WindowUpdate { difference } => {
@@ -157,7 +157,7 @@ impl P2pNetworkYamuxState {
157157
// try send as many frames as can
158158
let mut window = stream.window_theirs;
159159
while let Some(frame) = stream.pending.pop_front() {
160-
let len = frame.len() as u32;
160+
let len = frame.len_as_u32();
161161
pending_outgoing.push_back(frame);
162162
if let Some(new_window) = window.checked_sub(len) {
163163
window = new_window;
@@ -285,9 +285,9 @@ impl P2pNetworkYamuxState {
285285
return Ok(());
286286
};
287287
match &mut frame.inner {
288-
YamuxFrameInner::Data(data) => {
288+
YamuxFrameInner::Data(_) => {
289289
if let Some(new_window) =
290-
stream.window_theirs.checked_sub(data.len() as u32)
290+
stream.window_theirs.checked_sub(frame.len_as_u32())
291291
{
292292
// their window is big enough, decrease the size
293293
// and send the whole frame

p2p/src/network/yamux/p2p_network_yamux_state.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,15 @@ impl YamuxFrame {
343343
}
344344
}
345345

346+
// When we parse the frame we parse length as u32 and so `data.len()` should always be representable as u32
347+
pub fn len_as_u32(&self) -> u32 {
348+
if let YamuxFrameInner::Data(data) = &self.inner {
349+
u32::try_from(data.len()).unwrap_or(u32::MAX)
350+
} else {
351+
0
352+
}
353+
}
354+
346355
/// If this data is bigger then `pos`, keep only first `pos` bytes and return some remaining
347356
/// otherwise return none
348357
pub fn split_at(&mut self, pos: usize) -> Option<Self> {

0 commit comments

Comments
 (0)