]> git.lizzy.rs Git - rust.git/blob - tests/run-pass/errors.rs
e123538f6ce0dc2fbe6235b973bd111dac2a20d4
[rust.git] / tests / run-pass / errors.rs
1 #![crate_type = "lib"]
2 #![feature(custom_attribute)]
3 #![allow(dead_code, unused_attributes)]
4
5 #[miri_run]
6 fn overwriting_part_of_relocation_makes_the_rest_undefined() -> i32 {
7     let mut p = &42;
8     unsafe {
9         let ptr: *mut _ = &mut p;
10         *(ptr as *mut u32) = 123;
11     }
12     *p
13 }
14
15 #[miri_run]
16 fn pointers_to_different_allocations_are_unorderable() -> bool {
17     let x: *const u8 = &1;
18     let y: *const u8 = &2;
19     x < y
20 }
21
22 #[miri_run]
23 fn invalid_bool() -> u8 {
24     let b = unsafe { std::mem::transmute::<u8, bool>(2) };
25     if b { 1 } else { 2 }
26 }
27
28 #[miri_run]
29 fn undefined_byte_read() -> u8 {
30     let v: Vec<u8> = Vec::with_capacity(10);
31     let undef = unsafe { *v.get_unchecked(5) };
32     undef + 1
33 }
34
35 #[miri_run]
36 fn out_of_bounds_read() -> u8 {
37     let v: Vec<u8> = vec![1, 2];
38     unsafe { *v.get_unchecked(5) }
39 }
40
41 #[miri_run]
42 fn dangling_pointer_deref() -> i32 {
43     let p = {
44         let b = Box::new(42);
45         &*b as *const i32
46     };
47     unsafe { *p }
48 }