Skip to content

Commit

Permalink
tokio: check page capacity before obtaining base pointer (#4731)
Browse files Browse the repository at this point in the history
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 <dkoloski@google.com>
  • Loading branch information
djkoloski and David Koloski committed Jun 1, 2022
1 parent 925314b commit cc6c2f4
Showing 1 changed file with 2 additions and 3 deletions.
5 changes: 2 additions & 3 deletions tokio/src/util/slab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -551,10 +551,9 @@ impl<T> Slots<T> {
fn index_for(&self, slot: *const Value<T>) -> 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::<Slot<T>>();

Expand Down

0 comments on commit cc6c2f4

Please sign in to comment.