rust-aliasable icon indicating copy to clipboard operation
rust-aliasable copied to clipboard

Variance of AliasableMut is incorrect

Open talchas opened this issue 5 years ago • 5 comments

It uses NonNull<T> and PhantomData<&'a ()> which results in being covariant in T. A mutable pointer/reference must be invariant in T:

use aliasable::AliasableMut;
fn invalid<'a, 'b>(x: &'a mut &'static i32) -> &'a mut &'b i32 {
    AliasableMut::into_unique(AliasableMut::from_unique(x))
}

fn explode(x: &mut &'static i32) {
    let y = 2;
    *invalid(x) = &y;
}

fn main() {
    let mut x = &1;
    explode(&mut x);
    println!("{}", x);
}

prints 32767 on my machine.

talchas avatar Jul 03 '21 23:07 talchas

Thanks, good find! Really should have picked this up.

avitex avatar Jul 13 '21 03:07 avitex

This should be fixed in https://github.com/avitex/rust-aliasable/commit/4daf4bcb598116fd53cfeb00722dcd99d55fd303, but I want to just check a few more things before I push a release

avitex avatar Jul 13 '21 03:07 avitex

Yeah, that should be correct.

talchas avatar Jul 13 '21 04:07 talchas

@avitex

Would you mind eventually releasing a version of this crate that includes this soundness fix?

steffahn avatar Oct 16 '22 02:10 steffahn

ping @avitex , can you do a release?

jayvdb avatar Nov 17 '25 02:11 jayvdb