From efa36481f419152b53427e414e5b5b63ccb88d57 Mon Sep 17 00:00:00 2001 From: Vladislav Melnik Date: Mon, 7 Oct 2024 18:18:34 +0200 Subject: [PATCH] fix(yamux): avoid overflow --- p2p/src/network/yamux/p2p_network_yamux_reducer.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/p2p/src/network/yamux/p2p_network_yamux_reducer.rs b/p2p/src/network/yamux/p2p_network_yamux_reducer.rs index 5899a9f102..fad0dedd58 100644 --- a/p2p/src/network/yamux/p2p_network_yamux_reducer.rs +++ b/p2p/src/network/yamux/p2p_network_yamux_reducer.rs @@ -208,7 +208,8 @@ impl P2pNetworkYamuxState { if let Some(stream) = yamux_state.streams.get_mut(&frame.stream_id) { // must not underflow // TODO: check it and disconnect peer that violates flow rules - stream.window_ours -= data.len() as u32; + stream.window_ours = + stream.window_ours.wrapping_sub(data.len() as u32); } } YamuxFrameInner::WindowUpdate { difference } => { @@ -334,7 +335,7 @@ impl P2pNetworkYamuxState { // must not underflow // the action must not dispatch if it doesn't fit in the window // TODO: add pending queue, where frames will wait for window increase - stream.window_theirs -= data.len() as u32; + stream.window_theirs = stream.window_theirs.wrapping_sub(data.len() as u32); } YamuxFrameInner::WindowUpdate { difference } => { stream.update_window(true, *difference); @@ -417,11 +418,11 @@ impl YamuxStreamState { if *window < decreasing { *window = 0; } else { - *window -= decreasing; + *window = (*window).wrapping_sub(decreasing); } } else { let increasing = difference as u32; - *window += increasing; + *window = (*window).wrapping_add(increasing); } } }