linebender / linebender/druid

`Counter::next_nonzero` can have undefined behavior

Open
#2,198 2 comments 0 reactions 0 assignees View on GitHub

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.