Skip to content
Open
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
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]
## [v0.8.1] - 2026-04-15

### Fixed

- spsc: Fix integer overflow in iterators when N > usize::MAX/2 and the queue loops.
- spsc: Fix integer overflow leading to a panic in `len` when N == usize::MAX and debug assertions are enabled.

## [v0.8.0] - 2023-11-07

Expand Down Expand Up @@ -564,6 +569,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Initial release

[Unreleased]: https://github.com/rust-embedded/heapless/compare/v0.8.0...HEAD
[v0.8.1]: https://github.com/rust-embedded/heapless/compare/v0.8.0...v0.8.1
[v0.8.0]: https://github.com/rust-embedded/heapless/compare/v0.7.16...v0.8.0
[v0.7.16]: https://github.com/rust-embedded/heapless/compare/v0.7.15...v0.7.16
[v0.7.15]: https://github.com/rust-embedded/heapless/compare/v0.7.14...v0.7.15
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ keywords = ["static", "no-heap"]
license = "MIT OR Apache-2.0"
name = "heapless"
repository = "https://github.com/rust-embedded/heapless"
version = "0.8.0"
version = "0.8.1"

[features]
# Enable polyfilling of atomics via `portable-atomic`.
Expand Down
2 changes: 1 addition & 1 deletion build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![deny(warnings)]
// #![deny(warnings)]

use std::{
env,
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
#![cfg_attr(docsrs, feature(doc_cfg), feature(doc_auto_cfg))]
#![cfg_attr(not(test), no_std)]
#![deny(missing_docs)]
#![deny(warnings)]
//#![deny(warnings)]

pub use binary_heap::BinaryHeap;
pub use deque::Deque;
Expand Down
102 changes: 97 additions & 5 deletions src/spsc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,10 @@ impl<T, const N: usize> Queue<T, N> {

#[inline]
fn increment(val: usize) -> usize {
(val + 1) % N
// We know that N <= usize::MAX
// So this can only overflow if N == usize::MAX
// and in this case the overflow will be equivalent to the modulo N operation
val.wrapping_add(1) % N
}

/// Creates an empty queue with a fixed capacity of `N - 1`
Expand Down Expand Up @@ -375,7 +378,11 @@ impl<'a, T, const N: usize> Iterator for Iter<'a, T, N> {
if self.index < self.len {
let head = self.rb.head.load(Ordering::Relaxed);

let i = (head + self.index) % N;
let i = match head.checked_add(self.index) {
Some(i) if i >= N => i - N,
Some(i) => i,
None => head.wrapping_add(self.index).wrapping_sub(N),
};
self.index += 1;

Some(unsafe { &*(self.rb.buffer.get_unchecked(i).get() as *const T) })
Expand All @@ -392,7 +399,11 @@ impl<'a, T, const N: usize> Iterator for IterMut<'a, T, N> {
if self.index < self.len {
let head = self.rb.head.load(Ordering::Relaxed);

let i = (head + self.index) % N;
let i = match head.checked_add(self.index) {
Some(i) if i >= N => i - N,
Some(i) => i,
None => head.wrapping_add(self.index).wrapping_sub(N),
};
self.index += 1;

Some(unsafe { &mut *(self.rb.buffer.get_unchecked(i).get() as *mut T) })
Expand All @@ -408,7 +419,11 @@ impl<'a, T, const N: usize> DoubleEndedIterator for Iter<'a, T, N> {
let head = self.rb.head.load(Ordering::Relaxed);

// self.len > 0, since it's larger than self.index > 0
let i = (head + self.len - 1) % N;
let i = match head.checked_add(self.len - 1) {
Some(i) if i >= N => i - N,
Some(i) => i,
None => head.wrapping_add(self.len - 1).wrapping_sub(N),
};
self.len -= 1;
Some(unsafe { &*(self.rb.buffer.get_unchecked(i).get() as *const T) })
} else {
Expand All @@ -423,7 +438,11 @@ impl<'a, T, const N: usize> DoubleEndedIterator for IterMut<'a, T, N> {
let head = self.rb.head.load(Ordering::Relaxed);

// self.len > 0, since it's larger than self.index > 0
let i = (head + self.len - 1) % N;
let i = match head.checked_add(self.len - 1) {
Some(i) if i >= N => i - N,
Some(i) => i,
None => head.wrapping_add(self.len - 1).wrapping_sub(N),
};
self.len -= 1;
Some(unsafe { &mut *(self.rb.buffer.get_unchecked(i).get() as *mut T) })
} else {
Expand Down Expand Up @@ -592,7 +611,10 @@ impl<'a, T, const N: usize> Producer<'a, T, N> {
mod tests {
use std::hash::{Hash, Hasher};

use super::AtomicUsize;
use crate::spsc::Queue;
use core::cell::UnsafeCell;
use core::mem::MaybeUninit;

#[test]
fn full() {
Expand Down Expand Up @@ -905,4 +927,74 @@ mod tests {
};
assert_eq!(hash1, hash2);
}

// Test for some integer overflow bugs. See
// https://github.com/rust-embedded/heapless/pull/652#discussion_r3046630717
// for more info
#[test]
#[cfg_attr(miri, ignore)] // too slow
fn test_len_overflow() {
let mut queue = Queue::<(), { usize::MAX }> {
head: AtomicUsize::new(usize::MAX),
tail: AtomicUsize::new(2),
buffer: [const { UnsafeCell::new(MaybeUninit::new(())) }; usize::MAX],
};
queue.enqueue(()).unwrap();
queue.enqueue(()).unwrap();

let collected: Vec<_> = queue.iter().collect();
assert_eq!(&collected, &[&(); 4]);
}

#[test]
#[cfg_attr(miri, ignore)] // too slow
fn test_usize_overflow_iter() {
let queue = Queue::<(), { usize::MAX - 1 }> {
head: AtomicUsize::new(usize::MAX - 3),
tail: AtomicUsize::new(2),
buffer: [const { UnsafeCell::new(MaybeUninit::new(())) }; usize::MAX - 1],
};

let collected: Vec<_> = queue.iter().collect();
assert_eq!(&collected, &[&(); 4]);
}

#[test]
#[cfg_attr(miri, ignore)] // too slow
fn test_usize_overflow_iter_mut() {
let mut queue = Queue::<(), { usize::MAX - 1 }> {
head: AtomicUsize::new(usize::MAX - 3),
tail: AtomicUsize::new(2),
buffer: [const { UnsafeCell::new(MaybeUninit::new(())) }; usize::MAX - 1],
};

let collected: Vec<_> = queue.iter_mut().collect();
assert_eq!(&collected, &[&(); 4]);
}

#[test]
#[cfg_attr(miri, ignore)] // too slow
fn test_usize_overflow_iter_rev() {
let queue = Queue::<(), { usize::MAX - 1 }> {
head: AtomicUsize::new(usize::MAX - 3),
tail: AtomicUsize::new(2),
buffer: [const { UnsafeCell::new(MaybeUninit::new(())) }; usize::MAX - 1],
};

let collected: Vec<_> = queue.iter().rev().collect();
assert_eq!(&collected, &[&(); 4]);
}

#[test]
#[cfg_attr(miri, ignore)] // too slow
fn test_usize_overflow_iter_mut_rev() {
let mut queue = Queue::<(), { usize::MAX - 1 }> {
head: AtomicUsize::new(usize::MAX - 3),
tail: AtomicUsize::new(2),
buffer: [const { UnsafeCell::new(MaybeUninit::new(())) }; usize::MAX - 1],
};

let collected: Vec<_> = queue.iter_mut().rev().collect();
assert_eq!(&collected, &[&(); 4]);
}
}
2 changes: 1 addition & 1 deletion tests/tsan.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![deny(rust_2018_compatibility)]
#![deny(rust_2018_idioms)]
#![deny(warnings)]
//#![deny(warnings)]

use std::thread;

Expand Down