rust/tests/compile-fail/errors.rs

50 lines
1.3 KiB
Rust
Raw Normal View History

#![feature(custom_attribute)]
#![allow(dead_code, unused_attributes)]
#[miri_run]
fn overwriting_part_of_relocation_makes_the_rest_undefined() -> i32 {
let mut p = &42;
unsafe {
let ptr: *mut _ = &mut p;
*(ptr as *mut u32) = 123;
}
2016-04-22 03:34:14 -05:00
*p //~ ERROR: attempted to read undefined bytes
}
#[miri_run]
fn pointers_to_different_allocations_are_unorderable() -> bool {
let x: *const u8 = &1;
let y: *const u8 = &2;
2016-04-22 03:34:14 -05:00
x < y //~ ERROR: attempted to do math or a comparison on pointers into different allocations
}
2016-04-06 05:27:09 -05:00
#[miri_run]
2016-04-07 04:02:02 -05:00
fn invalid_bool() -> u8 {
2016-04-06 05:27:09 -05:00
let b = unsafe { std::mem::transmute::<u8, bool>(2) };
2016-04-22 03:34:14 -05:00
if b { 1 } else { 2 } //~ ERROR: invalid boolean value read
2016-04-06 05:27:09 -05:00
}
#[miri_run]
2016-04-07 04:02:02 -05:00
fn undefined_byte_read() -> u8 {
let v: Vec<u8> = Vec::with_capacity(10);
let undef = unsafe { *v.get_unchecked(5) };
2016-04-22 03:34:14 -05:00
undef + 1 //~ ERROR: attempted to read undefined bytes
}
2016-04-06 05:43:06 -05:00
#[miri_run]
2016-04-07 04:02:02 -05:00
fn out_of_bounds_read() -> u8 {
2016-04-06 05:43:06 -05:00
let v: Vec<u8> = vec![1, 2];
2016-04-22 03:34:14 -05:00
unsafe { *v.get_unchecked(5) } //~ ERROR: pointer offset outside bounds of allocation
2016-04-06 05:43:06 -05:00
}
2016-04-07 04:02:02 -05:00
#[miri_run]
fn dangling_pointer_deref() -> i32 {
let p = {
let b = Box::new(42);
&*b as *const i32
};
2016-04-22 03:34:14 -05:00
unsafe { *p } //~ ERROR: dangling pointer was dereferenced
2016-04-07 04:02:02 -05:00
}
2016-04-22 03:34:14 -05:00
fn main() {}