`Counter::next_nonzero` can have undefined behavior
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 9.7k
- Forks
- 565
- PR merge metrics
- No merged PRs in 30d
Description
Per the documentation of [AtomicU64::fetch_add](https://doc.rust-lang.org/core/sync/atomic/struct.AtomicU64.html#method.fetch_add)
> This operation wraps around on overflow.
This means that the following function will return zero if fetch_add is called U64::MAX times which violates the safety requirements for `NonZeroU64::new_unchecked`.
```rust
/// Return the next value, as a `NonZeroU64`.
pub fn next_nonzero(&self) -> NonZeroU64 {
// safe because our initial value is 1 and can only be incremented.
unsafe { NonZeroU64::new_unchecked(self.0.fetch_add(1, Ordering::Relaxed)) }
}
```
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at Counter::next_nonzero and review the AtomicU64::fetch_add behavior against the NonZeroU64 safety requirement. Trace the counter's initialization and overflow path; the issue is resolved when repeated increments cannot produce a zero value for NonZeroU64.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100