Skip to content

Commit 0f04d2a

Browse files
authored
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](https://github.com/rust-lang/rust/blob/b9aa9def858cfc66d411972b10ce3d98479acd78/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.
2 parents 2865128 + 0ea0c90 commit 0f04d2a

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

tests/ui/regions/regions-mock-codegen.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,23 @@ struct Ccx {
2222
x: isize,
2323
}
2424

25-
fn allocate(_bcx: &arena) -> &Bcx<'_> {
25+
fn allocate(_bcx: &arena) -> &mut Bcx<'_> {
2626
unsafe {
2727
let layout = Layout::new::<Bcx>();
2828
let ptr = Global.allocate(layout).unwrap_or_else(|_| handle_alloc_error(layout));
29-
&*(ptr.as_ptr() as *const _)
29+
&mut *ptr.as_ptr().cast()
3030
}
3131
}
3232

33-
fn h<'a>(bcx: &'a Bcx<'a>) -> &'a Bcx<'a> {
33+
fn h<'a>(bcx: &'a Bcx<'a>) -> &'a mut Bcx<'a> {
3434
return allocate(bcx.fcx.arena);
3535
}
3636

3737
fn g(fcx: &Fcx) {
3838
let bcx = Bcx { fcx };
3939
let bcx2 = h(&bcx);
4040
unsafe {
41-
Global.deallocate(NonNull::new_unchecked(bcx2 as *const _ as *mut _), Layout::new::<Bcx>());
41+
Global.deallocate(NonNull::new_unchecked(bcx2 as *mut _ as *mut _), Layout::new::<Bcx>());
4242
}
4343
}
4444

0 commit comments

Comments
 (0)