Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
protocols/kad: Fix right shift overflow panic in record_received (#1492)
* protocols/kad: Add test to reproduce right shift overflow panic * protocols/kad: Fix right shift overflow panic in record_received Within `Behaviour::record_received` the exponentially decreasing expiration based on the distance to the target for a record is calculated as following: 1. Calculate the amount of nodes between us and the record key beyond the k replication constant as `n`. 2. Shift the configured record time-to-live `n` times to the right to calculate an exponentially decreasing expiration. The configured record time-to-live is a u64. If `n` is larger or equal to 64 the right shift will lead to an overflow which panics in debug mode. This patch uses a checked right shift instead, defaulting to 0 (`now + 0`) for the expiration on overflow. * protocols/kad: Put attribute below comment * protocols/kad: Extract shifting logic and rework test Extract right shift into isolated function and replace complex regression test with small isolated one. * protocols/kad/src/behaviour: Refactor exp_decr_expiration Co-authored-by: Roman Borschel <romanb@users.noreply.github.com>
- Loading branch information