Skip to content

Commit

Permalink
AtomicPosition: fix overflowing addition
Browse files Browse the repository at this point in the history
  • Loading branch information
arxanas committed Mar 18, 2022
1 parent 10a37d4 commit 786ae0c
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ impl AtomicPosition {
let (new, remainder) = ((diff / INTERVAL), (diff % INTERVAL));
// We add `new` to `capacity`, subtract one for returning `true` from here,
// then make sure it does not exceed a maximum of `MAX_BURST`.
capacity = Ord::min(MAX_BURST, capacity + new as u8 - 1);
capacity = Ord::min(MAX_BURST, u64::from(capacity).saturating_add(new) as u8 - 1);

// Then, we just store `capacity` and `prev` atomically for the next iteration
self.capacity.store(capacity, Ordering::Release);
Expand Down Expand Up @@ -575,4 +575,12 @@ mod tests {
// Should not panic.
pb.set_position(0);
}

#[test]
fn test_atomic_position_large_time_difference() {
let atomic_position = AtomicPosition::new();
let later = atomic_position.start + Duration::from_nanos(INTERVAL * (u8::MAX as u64));
// Should not panic.
atomic_position.allow(later);
}
}

0 comments on commit 786ae0c

Please sign in to comment.