diff options
author | 2023-06-14 18:23:10 +0000 | |
---|---|---|
committer | 2023-06-14 18:23:10 +0000 | |
commit | bd67d2aaa5ddf2f4a0717fa0fd888c66189aa4e7 (patch) | |
tree | 5e2c22f4aae76eb9ecf296cb51624d44150e407c | |
parent | 599793829377a64603e93a1136360f17d3bade93 (diff) | |
parent | db18c00c00deb146478de1b0f94f8181300c47ce (diff) | |
download | rtic-bd67d2aaa5ddf2f4a0717fa0fd888c66189aa4e7.tar.gz rtic-bd67d2aaa5ddf2f4a0717fa0fd888c66189aa4e7.tar.zst rtic-bd67d2aaa5ddf2f4a0717fa0fd888c66189aa4e7.zip |
Merge #768
768: rtic-sync: Fix possible UB in make_channel! r=datdenkikniet a=korken89
Closes #763
Co-authored-by: Emil Fresk <emil.fresk@gmail.com>
-rw-r--r-- | rtic-sync/CHANGELOG.md | 6 | ||||
-rw-r--r-- | rtic-sync/Cargo.toml | 2 | ||||
-rw-r--r-- | rtic-sync/src/channel.rs | 21 |
3 files changed, 28 insertions, 1 deletions
diff --git a/rtic-sync/CHANGELOG.md b/rtic-sync/CHANGELOG.md index d3a9d846..65dd0a39 100644 --- a/rtic-sync/CHANGELOG.md +++ b/rtic-sync/CHANGELOG.md @@ -13,4 +13,10 @@ For each category, *Added*, *Changed*, *Fixed* add new entries at the top! ### Fixed +## [v1.0.1] + +### Fixed + +- `make_channel` could be UB + ## [v1.0.0] - 2023-xx-xx diff --git a/rtic-sync/Cargo.toml b/rtic-sync/Cargo.toml index ccb6cab5..f01cfbe4 100644 --- a/rtic-sync/Cargo.toml +++ b/rtic-sync/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rtic-sync" -version = "1.0.0" +version = "1.0.1" edition = "2021" authors = [ diff --git a/rtic-sync/src/channel.rs b/rtic-sync/src/channel.rs index 8c9f861d..06a6639b 100644 --- a/rtic-sync/src/channel.rs +++ b/rtic-sync/src/channel.rs @@ -106,6 +106,16 @@ macro_rules! make_channel { static mut CHANNEL: $crate::channel::Channel<$type, $size> = $crate::channel::Channel::new(); + static CHECK: ::core::sync::atomic::AtomicU8 = ::core::sync::atomic::AtomicU8::new(0); + + critical_section::with(|_| { + if CHECK.load(::core::sync::atomic::Ordering::Relaxed) != 0 { + panic!("call to the same `make_channel` instance twice"); + } + + CHECK.store(1, ::core::sync::atomic::Ordering::Relaxed); + }); + // SAFETY: This is safe as we hide the static mut from others to access it. // Only this point is where the mutable access happens. unsafe { CHANNEL.split() } @@ -573,4 +583,15 @@ mod tests { v.await.unwrap(); } } + + fn make() { + let _ = make_channel!(u32, 10); + } + + #[test] + #[should_panic] + fn double_make_channel() { + make(); + make(); + } } |