Rollup merge of #107955 - RalfJung:ancient-ub, r=jyn514

fix UB in ancient test

This seems to go back all the way to the [original version of this test](b9aa9def85/src/test/run-pass/regions-mock-trans.rs) from ten years ago... ``@nikomatsakis`` trip down memory lane? ;)

Clearly deallocation is a form of mutation so doing it to a (pointer derived from a) shared reference cannot be legal. Let's use mutable references instead.
This commit is contained in:
Matthias Krüger 2023-02-12 22:29:49 +01:00 committed by GitHub
commit f81cb97cb2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -22,15 +22,15 @@ struct Ccx {
x: isize, x: isize,
} }
fn allocate(_bcx: &arena) -> &Bcx<'_> { fn allocate(_bcx: &arena) -> &mut Bcx<'_> {
unsafe { unsafe {
let layout = Layout::new::<Bcx>(); let layout = Layout::new::<Bcx>();
let ptr = Global.allocate(layout).unwrap_or_else(|_| handle_alloc_error(layout)); let ptr = Global.allocate(layout).unwrap_or_else(|_| handle_alloc_error(layout));
&*(ptr.as_ptr() as *const _) &mut *ptr.as_ptr().cast()
} }
} }
fn h<'a>(bcx: &'a Bcx<'a>) -> &'a Bcx<'a> { fn h<'a>(bcx: &'a Bcx<'a>) -> &'a mut Bcx<'a> {
return allocate(bcx.fcx.arena); return allocate(bcx.fcx.arena);
} }
@ -38,7 +38,7 @@ fn g(fcx: &Fcx) {
let bcx = Bcx { fcx }; let bcx = Bcx { fcx };
let bcx2 = h(&bcx); let bcx2 = h(&bcx);
unsafe { unsafe {
Global.deallocate(NonNull::new_unchecked(bcx2 as *const _ as *mut _), Layout::new::<Bcx>()); Global.deallocate(NonNull::new_unchecked(bcx2 as *mut _ as *mut _), Layout::new::<Bcx>());
} }
} }