Skip to content

Commit 78f045d

Browse files
authored
Merge pull request #780 from openmina/fix/yamux-overflow
Avoid overflow
2 parents 4710464 + efa3648 commit 78f045d

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

p2p/src/network/yamux/p2p_network_yamux_reducer.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,8 @@ impl P2pNetworkYamuxState {
208208
if let Some(stream) = yamux_state.streams.get_mut(&frame.stream_id) {
209209
// must not underflow
210210
// TODO: check it and disconnect peer that violates flow rules
211-
stream.window_ours -= data.len() as u32;
211+
stream.window_ours =
212+
stream.window_ours.wrapping_sub(data.len() as u32);
212213
}
213214
}
214215
YamuxFrameInner::WindowUpdate { difference } => {
@@ -334,7 +335,7 @@ impl P2pNetworkYamuxState {
334335
// must not underflow
335336
// the action must not dispatch if it doesn't fit in the window
336337
// TODO: add pending queue, where frames will wait for window increase
337-
stream.window_theirs -= data.len() as u32;
338+
stream.window_theirs = stream.window_theirs.wrapping_sub(data.len() as u32);
338339
}
339340
YamuxFrameInner::WindowUpdate { difference } => {
340341
stream.update_window(true, *difference);
@@ -417,11 +418,11 @@ impl YamuxStreamState {
417418
if *window < decreasing {
418419
*window = 0;
419420
} else {
420-
*window -= decreasing;
421+
*window = (*window).wrapping_sub(decreasing);
421422
}
422423
} else {
423424
let increasing = difference as u32;
424-
*window += increasing;
425+
*window = (*window).wrapping_add(increasing);
425426
}
426427
}
427428
}

0 commit comments

Comments
 (0)