Skip to content

fix: replace static mut ID_COUNTER with AtomicU64 - #75

Open
Jay-Jay-Tee wants to merge 1 commit into
shaaravraghu:mainfrom
Jay-Jay-Tee:fix/static-mut-id-counter
Open

fix: replace static mut ID_COUNTER with AtomicU64#75
Jay-Jay-Tee wants to merge 1 commit into
shaaravraghu:mainfrom
Jay-Jay-Tee:fix/static-mut-id-counter

Conversation

@Jay-Jay-Tee

Copy link
Copy Markdown
Contributor

Closes #62

Problem

next_id() in src/storage/ram.rs uses a raw static mut to generate entry IDs. In Rust, any access to static mut is undefined behavior per the reference and Miri, even from a single thread. The compiler is free to reorder, cache, or elide reads and writes to it.

The app also uses Arc<RwLock> and a tokio runtime, so next_id() can be called from multiple threads, making this a data race that could produce duplicate or skipped IDs and silently corrupt clipboard history and vault storage lookups.

Fix

Replace with AtomicU64::fetch_add:

static ID_COUNTER: AtomicU64 = AtomicU64::new(0);

pub fn next_id() -> EntryId {
    ID_COUNTER.fetch_add(1, Ordering::Relaxed)
}

No unsafe block needed. Thread-safe. Zero runtime overhead.

Files changed

  • src/storage/ram.rs

static mut is undefined behavior in Rust regardless of threading.
The app also uses Arc<RwLock> and a tokio runtime, so next_id()
can be called from multiple threads, making this a data race that
could produce duplicate or skipped IDs and silently corrupt
clipboard history and vault storage lookups.

Replace with AtomicU64::fetch_add which is thread-safe, needs no
unsafe block, and has zero runtime overhead.

Closes shaaravraghu#62
@Jay-Jay-Tee

Copy link
Copy Markdown
Contributor Author

@shaaravraghu, do check this out!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Undefined Behavior in next_id() - static mut ID_COUNTER accessed without synchronization

1 participant