Skip to content

Commit bd67d2a

Browse files
bors[bot]korken89
andauthored
Merge #768
768: rtic-sync: Fix possible UB in make_channel! r=datdenkikniet a=korken89 Closes #763 Co-authored-by: Emil Fresk <[email protected]>
2 parents 5997938 + db18c00 commit bd67d2a

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

rtic-sync/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,10 @@ For each category, *Added*, *Changed*, *Fixed* add new entries at the top!
1313

1414
### Fixed
1515

16+
## [v1.0.1]
17+
18+
### Fixed
19+
20+
- `make_channel` could be UB
21+
1622
## [v1.0.0] - 2023-xx-xx

rtic-sync/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rtic-sync"
3-
version = "1.0.0"
3+
version = "1.0.1"
44

55
edition = "2021"
66
authors = [

rtic-sync/src/channel.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,16 @@ macro_rules! make_channel {
106106
static mut CHANNEL: $crate::channel::Channel<$type, $size> =
107107
$crate::channel::Channel::new();
108108

109+
static CHECK: ::core::sync::atomic::AtomicU8 = ::core::sync::atomic::AtomicU8::new(0);
110+
111+
critical_section::with(|_| {
112+
if CHECK.load(::core::sync::atomic::Ordering::Relaxed) != 0 {
113+
panic!("call to the same `make_channel` instance twice");
114+
}
115+
116+
CHECK.store(1, ::core::sync::atomic::Ordering::Relaxed);
117+
});
118+
109119
// SAFETY: This is safe as we hide the static mut from others to access it.
110120
// Only this point is where the mutable access happens.
111121
unsafe { CHANNEL.split() }
@@ -573,4 +583,15 @@ mod tests {
573583
v.await.unwrap();
574584
}
575585
}
586+
587+
fn make() {
588+
let _ = make_channel!(u32, 10);
589+
}
590+
591+
#[test]
592+
#[should_panic]
593+
fn double_make_channel() {
594+
make();
595+
make();
596+
}
576597
}

0 commit comments

Comments
 (0)