Skip to content

Commit 8903ff0

Browse files
bors[bot]sticnarftaiki-e
authored
Merge #940 #941
940: Fix get_unchecked panic by raw pointer calculation r=taiki-e a=sticnarf It is related to #878 but doesn't fix it. Miri still complains with this PR. It fixes the panic on recent toolchains (after rust-lang/rust#92686) when using `-Zbuild-std`. ``` $ RUST_BACKTRACE=1 cargo test -p crossbeam-skiplist -Zbuild-std --target x86_64-unknown-linux-gnu --test map -- smoke Finished test [unoptimized + debuginfo] target(s) in 0.03s Running tests/map.rs (target/x86_64-unknown-linux-gnu/debug/deps/map-509dc855f6125f07) running 1 test thread 'smoke' panicked at 'unsafe precondition(s) violated: slice::get_unchecked requires that the index is within the slice', /home/yilin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/panicking.rs:87:58 stack backtrace: 0: rust_begin_unwind at /home/yilin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/panicking.rs:575:5 1: core::panicking::panic_str_nounwind at /home/yilin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/panicking.rs:90:14 2: <usize as core::slice::index::SliceIndex<[T]>>::get_unchecked::runtime at /home/yilin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/intrinsics.rs:2284:21 3: <usize as core::slice::index::SliceIndex<[T]>>::get_unchecked at /home/yilin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/slice/index.rs:236:13 4: core::slice::<impl [T]>::get_unchecked at /home/yilin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/slice/mod.rs:399:20 5: <crossbeam_skiplist::base::Tower<K,V> as core::ops::index::Index<usize>>::index at ./src/base.rs:39:18 6: crossbeam_skiplist::base::SkipList<K,V>::search_position at ./src/base.rs:781:24 7: crossbeam_skiplist::base::SkipList<K,V>::insert_internal at ./src/base.rs:871:26 8: crossbeam_skiplist::base::SkipList<K,V>::insert at ./src/base.rs:1085:9 9: crossbeam_skiplist::map::SkipMap<K,V>::insert at ./src/map.rs:375:20 10: map::smoke at ./tests/map.rs:9:5 11: map::smoke::{{closure}} at ./tests/map.rs:7:12 12: core::ops::function::FnOnce::call_once at /home/yilin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:507:5 13: core::ops::function::FnOnce::call_once at /home/yilin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:507:5 note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace. thread panicked while panicking. aborting. error: test failed, to rerun pass `-p crossbeam-skiplist --test map` Caused by: process didn't exit successfully: `/home/yilin/Repos/crossbeam/target/x86_64-unknown-linux-gnu/debug/deps/map-509dc855f6125f07 smoke` (signal: 6, SIGABRT: process abort signal) ``` 941: Fix newly added clippy warnings r=taiki-e a=taiki-e Co-authored-by: Yilin Chen <sticnarf@gmail.com> Co-authored-by: Taiki Endo <te316e89@gmail.com>
3 parents 0de660e + c591923 + fd0ad9f commit 8903ff0

File tree

3 files changed

+5
-13
lines changed

3 files changed

+5
-13
lines changed

crossbeam-channel/src/flavors/at.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,7 @@ impl Channel {
134134
/// Returns the number of messages in the channel.
135135
#[inline]
136136
pub(crate) fn len(&self) -> usize {
137-
if self.is_empty() {
138-
0
139-
} else {
140-
1
141-
}
137+
usize::from(!self.is_empty())
142138
}
143139

144140
/// Returns the capacity of the channel.

crossbeam-channel/src/flavors/tick.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,7 @@ impl Channel {
105105
/// Returns the number of messages in the channel.
106106
#[inline]
107107
pub(crate) fn len(&self) -> usize {
108-
if self.is_empty() {
109-
0
110-
} else {
111-
1
112-
}
108+
usize::from(!self.is_empty())
113109
}
114110

115111
/// Returns the capacity of the channel.

crossbeam-skiplist/src/base.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ impl<K, V> Index<usize> for Tower<K, V> {
3636
fn index(&self, index: usize) -> &Atomic<Node<K, V>> {
3737
// This implementation is actually unsafe since we don't check if the
3838
// index is in-bounds. But this is fine since this is only used internally.
39-
unsafe { self.pointers.get_unchecked(index) }
39+
unsafe { &*(&self.pointers as *const Atomic<Node<K, V>>).add(index) }
4040
}
4141
}
4242

@@ -1718,7 +1718,7 @@ where
17181718
};
17191719
match (&next_head, &self.tail) {
17201720
// The next key is larger than the latest tail key we observed with this iterator.
1721-
(Some(ref next), &Some(ref t)) if next.key() >= t.key() => {
1721+
(Some(ref next), Some(t)) if next.key() >= t.key() => {
17221722
unsafe {
17231723
next.node.decrement(guard);
17241724
}
@@ -1745,7 +1745,7 @@ where
17451745
};
17461746
match (&self.head, &next_tail) {
17471747
// The prev key is smaller than the latest head key we observed with this iterator.
1748-
(&Some(ref h), Some(next)) if h.key() >= next.key() => {
1748+
(Some(h), Some(next)) if h.key() >= next.key() => {
17491749
unsafe {
17501750
next.node.decrement(guard);
17511751
}

0 commit comments

Comments
 (0)