Skip to content

Commit 959fc9b

Browse files
committed
CAS Pool: make dangling Ptr well aligned
the old logic didn't consider the pointee's alignment when creating a dangling pointer dangling pointers are used in pools of ZST (Zero Sized Types). the old logic resulted in these Boxed ZST not being well aligned. the test added in this PR was failing
1 parent 9563b35 commit 959fc9b

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/pool/cas.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ impl<T> Ptr<T> {
177177
}
178178

179179
pub fn dangling() -> Self {
180-
unsafe { Self::from_parts(initial_tag_value(), 1) }
180+
unsafe { Self::from_parts(initial_tag_value(), core::mem::align_of::<T>() as i32) }
181181
}
182182

183183
pub unsafe fn as_ref(&self) -> &T {

src/pool/singleton.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,17 @@ mod tests {
354354
assert_eq!(*A::alloc().unwrap().init(1), 1);
355355
}
356356

357+
#[test]
358+
fn boxed_zst_is_well_aligned() {
359+
#[repr(align(2))]
360+
pub struct Zst2;
361+
362+
pool!(A: Zst2);
363+
364+
let x = A::alloc().unwrap().init(Zst2);
365+
assert_eq!(0, &*x as *const Zst2 as usize % 2);
366+
}
367+
357368
#[test]
358369
fn destructors() {
359370
static COUNT: AtomicUsize = AtomicUsize::new(0);

0 commit comments

Comments
 (0)