Skip to content

Commit

Permalink
sync: add_permits does not panic with usize::MAX >> 3
Browse files Browse the repository at this point in the history
Fixed incorrect shift in the assignment of permits on batch_semaphore.rs

Fixes: #3095
  • Loading branch information
dungex committed Nov 28, 2020
1 parent 4912943 commit 0692831
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion tokio/src/sync/batch_semaphore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ impl Semaphore {
}

if rem > 0 && is_empty {
let permits = rem << Self::PERMIT_SHIFT;
let permits = rem >> Self::PERMIT_SHIFT;
assert!(
permits < Self::MAX_PERMITS,
"cannot add more than MAX_PERMITS permits ({})",
Expand Down
6 changes: 6 additions & 0 deletions tokio/tests/sync_semaphore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,9 @@ async fn stresstest() {
let _p5 = sem.try_acquire().unwrap();
assert!(sem.try_acquire().is_err());
}

#[test]
fn add_max_amount_permits() {
let s = tokio::sync::Semaphore::new(0);
s.add_permits(usize::MAX >> 3);
}

0 comments on commit 0692831

Please sign in to comment.