Closed
Description
Currently, in Rust, undefined behaviour can only appear within an unsafe block, with one exception: one can take a reference to the body of an Unsafe block when unsafe code had borrowed the inside of the block, as in the following Rust code:
use std::ty::Unsafe;
use std::kinds::marker::InvariantType;
fn mess_up_memory() {
let mut result : Vec<Box<uint>> = vec![];
for _ in range(0, 10000u) { result.push(box 0xcccccccc); }
}
fn main() {
// Create an unsafe object
let x = Unsafe { value: box 1u, marker1: InvariantType };
let mut_alias : &mut Box<uint> = unsafe { &mut *x.get() };
// The behaviour up to here was completely defined.
// The unsafe introduced no undefined behaviour
// Now, lets introduce some UB
let alias : &Box<uint> = &x.value;
// Now we have aliased & and &mut pointers
// Have fun
let internal : &uint = &**alias;
println!("internal={}", internal);
*mut_alias = box 2;
mess_up_memory();
println!("internal={}", internal);
}
This was confused with some issues with statics in rust-lang/rfcs#177 and #14862, but, as shown here, has nothing to do with them.
This is not an RFC because I don't have a plan to fix this (and would prefer to first talk about the issue here).
Metadata
Metadata
Assignees
Labels
No labels