From c59192389f8a196f02600cea6cf2f3ed50130b83 Mon Sep 17 00:00:00 2001 From: Yilin Chen Date: Fri, 16 Dec 2022 00:53:41 +0800 Subject: [PATCH 1/3] Fix get_unchecked UB by raw pointer calculation Signed-off-by: Yilin Chen --- crossbeam-skiplist/src/base.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crossbeam-skiplist/src/base.rs b/crossbeam-skiplist/src/base.rs index d27c531b5..7d9bf5119 100644 --- a/crossbeam-skiplist/src/base.rs +++ b/crossbeam-skiplist/src/base.rs @@ -36,7 +36,7 @@ impl Index for Tower { fn index(&self, index: usize) -> &Atomic> { // This implementation is actually unsafe since we don't check if the // index is in-bounds. But this is fine since this is only used internally. - unsafe { self.pointers.get_unchecked(index) } + unsafe { &*(&self.pointers as *const Atomic>).add(index) } } } From 374c5cd446d71d1e8695f315aaf96b0d4a1c7ad3 Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Fri, 16 Dec 2022 21:01:36 +0900 Subject: [PATCH 2/3] Fix clippy::bool_to_int_with_if warning ``` warning: boolean to int conversion using if --> crossbeam-channel/src/flavors/at.rs:137:9 | 137 | / if self.is_empty() { 138 | | 0 139 | | } else { 140 | | 1 141 | | } | |_________^ help: replace with from: `usize::from(!self.is_empty())` | = note: `!self.is_empty() as usize` or `(!self.is_empty()).into()` can also be valid options = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#bool_to_int_with_if = note: `#[warn(clippy::bool_to_int_with_if)]` on by default warning: boolean to int conversion using if --> crossbeam-channel/src/flavors/tick.rs:108:9 | 108 | / if self.is_empty() { 109 | | 0 110 | | } else { 111 | | 1 112 | | } | |_________^ help: replace with from: `usize::from(!self.is_empty())` | = note: `!self.is_empty() as usize` or `(!self.is_empty()).into()` can also be valid options = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#bool_to_int_with_if ``` --- crossbeam-channel/src/flavors/at.rs | 6 +----- crossbeam-channel/src/flavors/tick.rs | 6 +----- 2 files changed, 2 insertions(+), 10 deletions(-) diff --git a/crossbeam-channel/src/flavors/at.rs b/crossbeam-channel/src/flavors/at.rs index ca5ee60f5..070e989d6 100644 --- a/crossbeam-channel/src/flavors/at.rs +++ b/crossbeam-channel/src/flavors/at.rs @@ -134,11 +134,7 @@ impl Channel { /// Returns the number of messages in the channel. #[inline] pub(crate) fn len(&self) -> usize { - if self.is_empty() { - 0 - } else { - 1 - } + usize::from(!self.is_empty()) } /// Returns the capacity of the channel. diff --git a/crossbeam-channel/src/flavors/tick.rs b/crossbeam-channel/src/flavors/tick.rs index 4201b6eb0..484825dc8 100644 --- a/crossbeam-channel/src/flavors/tick.rs +++ b/crossbeam-channel/src/flavors/tick.rs @@ -105,11 +105,7 @@ impl Channel { /// Returns the number of messages in the channel. #[inline] pub(crate) fn len(&self) -> usize { - if self.is_empty() { - 0 - } else { - 1 - } + usize::from(!self.is_empty()) } /// Returns the capacity of the channel. From fd0ad9f5f94a5531eb6d7377b75385d71d89abd7 Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Fri, 16 Dec 2022 21:03:45 +0900 Subject: [PATCH 3/3] Fix clippy::needless_borrowed_reference warning ``` warning: dereferencing a tuple pattern where every element takes a reference --> crossbeam-skiplist/src/base.rs:1721:30 | 1721 | (Some(ref next), &Some(ref t)) if next.key() >= t.key() => { | ^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrowed_reference = note: `#[warn(clippy::needless_borrowed_reference)]` on by default help: try removing the `&` and `ref` parts | 1721 - (Some(ref next), &Some(ref t)) if next.key() >= t.key() => { 1721 + (Some(ref next), Some(t)) if next.key() >= t.key() => { | warning: dereferencing a tuple pattern where every element takes a reference --> crossbeam-skiplist/src/base.rs:1748:14 | 1748 | (&Some(ref h), Some(next)) if h.key() >= next.key() => { | ^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrowed_reference help: try removing the `&` and `ref` parts | 1748 - (&Some(ref h), Some(next)) if h.key() >= next.key() => { 1748 + (Some(h), Some(next)) if h.key() >= next.key() => { | ``` --- crossbeam-skiplist/src/base.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crossbeam-skiplist/src/base.rs b/crossbeam-skiplist/src/base.rs index d27c531b5..4dddcacda 100644 --- a/crossbeam-skiplist/src/base.rs +++ b/crossbeam-skiplist/src/base.rs @@ -1718,7 +1718,7 @@ where }; match (&next_head, &self.tail) { // The next key is larger than the latest tail key we observed with this iterator. - (Some(ref next), &Some(ref t)) if next.key() >= t.key() => { + (Some(ref next), Some(t)) if next.key() >= t.key() => { unsafe { next.node.decrement(guard); } @@ -1745,7 +1745,7 @@ where }; match (&self.head, &next_tail) { // The prev key is smaller than the latest head key we observed with this iterator. - (&Some(ref h), Some(next)) if h.key() >= next.key() => { + (Some(h), Some(next)) if h.key() >= next.key() => { unsafe { next.node.decrement(guard); }