From cc6c2f40cb1f54a6f53157a7631ac075130c7e36 Mon Sep 17 00:00:00 2001 From: David Koloski Date: Wed, 1 Jun 2022 15:18:06 -0400 Subject: [PATCH] tokio: check page capacity before obtaining base pointer (#4731) This doesn't cause any issues in practice because this is a private API that is only used in ways that cannot trigger UB. Indexing into `slots` is not sound until after we've asserted that the page is allocated, since that aliases the first slot which may not be allocated. This PR also switches to using `as_ptr` to obtain the base pointer for clarity. Co-authored-by: David Koloski --- tokio/src/util/slab.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tokio/src/util/slab.rs b/tokio/src/util/slab.rs index 214fa08dc..0e16e40e9 100644 --- a/tokio/src/util/slab.rs +++ b/tokio/src/util/slab.rs @@ -551,10 +551,9 @@ impl Slots { fn index_for(&self, slot: *const Value) -> usize { use std::mem; - let base = &self.slots[0] as *const _ as usize; - - assert!(base != 0, "page is unallocated"); + assert_ne!(self.slots.capacity(), 0, "page is unallocated"); + let base = self.slots.as_ptr() as usize; let slot = slot as usize; let width = mem::size_of::>();