Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(swarm): prevent overflow in keep-alive computation #4644

Merged
merged 11 commits into from
Oct 18, 2023
5 changes: 5 additions & 0 deletions swarm/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.43.7 - unreleased

- Fix overflow in `KeepAlive` computation that could occur panic at `Delay::reset` if `SwarmBuilder::idle_connection_timeout` is configured too large.
See [PR 4644](https://github.com/libp2p/rust-libp2p/pull/4644).

## 0.43.6 - unreleased

thomaseizinger marked this conversation as resolved.
Show resolved Hide resolved
- Deprecate `libp2p::swarm::SwarmBuilder`.
Expand Down
6 changes: 3 additions & 3 deletions swarm/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,13 +351,13 @@ where
(Shutdown::Later(timer, deadline), KeepAlive::Until(t)) => {
if *deadline != t {
*deadline = t;
let now = Instant::now();
if let Some(new_duration) = deadline.checked_duration_since(Instant::now())
{
let effective_keep_alive = max(new_duration, *idle_timeout);

let now = Instant::now();
let safe_effective_keep_alive = checked_add_fraction(now, effective_keep_alive);

// `Delay::reset` panics if `now + effective_keep_alive` is `> u64::MAX`.
let safe_effective_keep_alive = checked_add_fraction(now, effective_keep_alive);
timer.reset(safe_effective_keep_alive)
}
}
Expand Down