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 addition overflow in AtomicPosition #406

Merged
merged 2 commits into from
Mar 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/draw_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ impl RateLimiter {

// We add `new` to `capacity`, subtract one for returning `true` from here,
// then make sure it does not exceed a maximum of `MAX_BURST`, then store it.
self.capacity = Ord::min(MAX_BURST, self.capacity + new as u8 - 1);
self.capacity = Ord::min(MAX_BURST as u128, (self.capacity as u128) + new - 1) as u8;
// Store `prev` for the next iteration after subtracting the `remainder`.
self.prev = now - Duration::from_nanos(remainder as u64);
true
Expand Down
2 changes: 1 addition & 1 deletion src/progress_bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl ProgressBar {

/// Creates a new progress bar with a given length and draw target
pub fn with_draw_target(len: Option<u64>, draw_target: ProgressDrawTarget) -> ProgressBar {
let pos = Arc::new(AtomicPosition::default());
let pos = Arc::new(AtomicPosition::new());
ProgressBar {
state: Arc::new(Mutex::new(BarState::new(len, draw_target, pos.clone()))),
pos,
Expand Down
30 changes: 18 additions & 12 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,15 @@ pub(crate) struct AtomicPosition {
}

impl AtomicPosition {
pub(crate) fn new() -> Self {
Self {
pos: AtomicU64::new(0),
capacity: AtomicU8::new(MAX_BURST),
prev: AtomicU64::new(0),
start: Instant::now(),
}
}

pub(crate) fn allow(&self, now: Instant) -> bool {
if now < self.start {
return false;
Expand Down Expand Up @@ -445,7 +454,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 as u128, (capacity as u128) + (new as u128) - 1) as u8;

// Then, we just store `capacity` and `prev` atomically for the next iteration
self.capacity.store(capacity, Ordering::Release);
Expand All @@ -468,17 +477,6 @@ impl AtomicPosition {
}
}

impl Default for AtomicPosition {
fn default() -> Self {
Self {
pos: AtomicU64::new(0),
capacity: AtomicU8::new(MAX_BURST),
prev: AtomicU64::new(0),
start: Instant::now(),
}
}
}

const INTERVAL: u64 = 1_000_000;
const MAX_BURST: u8 = 10;

Expand Down Expand Up @@ -605,4 +603,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 * u64::from(u8::MAX));
// Should not panic.
atomic_position.allow(later);
}
}
6 changes: 3 additions & 3 deletions src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@ mod tests {
#[test]
fn test_expand_template() {
const WIDTH: u16 = 80;
let pos = Arc::new(AtomicPosition::default());
let pos = Arc::new(AtomicPosition::new());
let state = ProgressState::new(Some(10), pos);
let mut buf = Vec::new();

Expand All @@ -707,7 +707,7 @@ mod tests {
set_colors_enabled(true);

const WIDTH: u16 = 80;
let pos = Arc::new(AtomicPosition::default());
let pos = Arc::new(AtomicPosition::new());
let state = ProgressState::new(Some(10), pos);
let mut buf = Vec::new();

Expand Down Expand Up @@ -737,7 +737,7 @@ mod tests {
#[test]
fn align_truncation() {
const WIDTH: u16 = 10;
let pos = Arc::new(AtomicPosition::default());
let pos = Arc::new(AtomicPosition::new());
let state = ProgressState::new(Some(10), pos);
let mut buf = Vec::new();

Expand Down