|
| 1 | +// check-fail |
| 2 | +// run-rustfix |
| 3 | + |
| 4 | +use std::ptr; |
| 5 | +use std::mem; |
| 6 | + |
| 7 | +unsafe fn null_ptr() { |
| 8 | + ptr::write( |
| 9 | + //~^ ERROR calling this function with a null pointer is undefined behavior |
| 10 | + ptr::null_mut() as *mut u32, |
| 11 | + mem::transmute::<[u8; 4], _>([0, 0, 0, 255]), |
| 12 | + ); |
| 13 | + |
| 14 | + let null_ptr = ptr::null_mut(); |
| 15 | + ptr::write( |
| 16 | + //~^ ERROR calling this function with a null pointer is undefined behavior |
| 17 | + null_ptr as *mut u32, |
| 18 | + mem::transmute::<[u8; 4], _>([0, 0, 0, 255]), |
| 19 | + ); |
| 20 | + |
| 21 | + let _: &[usize] = std::slice::from_raw_parts(ptr::null(), 0); |
| 22 | + //~^ ERROR calling this function with a null pointer is undefined behavior |
| 23 | + let _: &[usize] = std::slice::from_raw_parts(ptr::null_mut(), 0); |
| 24 | + //~^ ERROR calling this function with a null pointer is undefined behavior |
| 25 | + let _: &[usize] = std::slice::from_raw_parts(0 as *mut _, 0); |
| 26 | + //~^ ERROR calling this function with a null pointer is undefined behavior |
| 27 | + let _: &[usize] = std::slice::from_raw_parts(mem::transmute(0usize), 0); |
| 28 | + //~^ ERROR calling this function with a null pointer is undefined behavior |
| 29 | + |
| 30 | + let _: &[usize] = std::slice::from_raw_parts_mut(ptr::null_mut(), 0); |
| 31 | + //~^ ERROR calling this function with a null pointer is undefined behavior |
| 32 | + |
| 33 | + ptr::copy::<usize>(ptr::null(), ptr::NonNull::dangling().as_ptr(), 0); |
| 34 | + //~^ ERROR calling this function with a null pointer is undefined behavior |
| 35 | + ptr::copy::<usize>(ptr::NonNull::dangling().as_ptr(), ptr::null_mut(), 0); |
| 36 | + //~^ ERROR calling this function with a null pointer is undefined behavior |
| 37 | + |
| 38 | + ptr::copy_nonoverlapping::<usize>(ptr::null(), ptr::NonNull::dangling().as_ptr(), 0); |
| 39 | + //~^ ERROR calling this function with a null pointer is undefined behavior |
| 40 | + ptr::copy_nonoverlapping::<usize>( |
| 41 | + //~^ ERROR calling this function with a null pointer is undefined behavior |
| 42 | + ptr::NonNull::dangling().as_ptr(), |
| 43 | + ptr::null_mut(), |
| 44 | + 0 |
| 45 | + ); |
| 46 | + |
| 47 | + #[derive(Copy, Clone)] |
| 48 | + struct A(usize); |
| 49 | + let mut v = A(200); |
| 50 | + |
| 51 | + let _a: A = ptr::read(ptr::null()); |
| 52 | + //~^ ERROR calling this function with a null pointer is undefined behavior |
| 53 | + let _a: A = ptr::read(ptr::null_mut()); |
| 54 | + //~^ ERROR calling this function with a null pointer is undefined behavior |
| 55 | + |
| 56 | + let _a: A = ptr::read_unaligned(ptr::null()); |
| 57 | + //~^ ERROR calling this function with a null pointer is undefined behavior |
| 58 | + let _a: A = ptr::read_unaligned(ptr::null_mut()); |
| 59 | + //~^ ERROR calling this function with a null pointer is undefined behavior |
| 60 | + |
| 61 | + let _a: A = ptr::read_volatile(ptr::null()); |
| 62 | + //~^ ERROR calling this function with a null pointer is undefined behavior |
| 63 | + let _a: A = ptr::read_volatile(ptr::null_mut()); |
| 64 | + //~^ ERROR calling this function with a null pointer is undefined behavior |
| 65 | + |
| 66 | + let _a: A = ptr::replace(ptr::null_mut(), v); |
| 67 | + //~^ ERROR calling this function with a null pointer is undefined behavior |
| 68 | + |
| 69 | + ptr::swap::<A>(ptr::null_mut(), &mut v); |
| 70 | + //~^ ERROR calling this function with a null pointer is undefined behavior |
| 71 | + ptr::swap::<A>(&mut v, ptr::null_mut()); |
| 72 | + //~^ ERROR calling this function with a null pointer is undefined behavior |
| 73 | + |
| 74 | + ptr::swap_nonoverlapping::<A>(ptr::null_mut(), &mut v, 0); |
| 75 | + //~^ ERROR calling this function with a null pointer is undefined behavior |
| 76 | + ptr::swap_nonoverlapping::<A>(&mut v, ptr::null_mut(), 0); |
| 77 | + //~^ ERROR calling this function with a null pointer is undefined behavior |
| 78 | + |
| 79 | + ptr::write(ptr::null_mut(), v); |
| 80 | + //~^ ERROR calling this function with a null pointer is undefined behavior |
| 81 | + |
| 82 | + ptr::write_unaligned(ptr::null_mut(), v); |
| 83 | + //~^ ERROR calling this function with a null pointer is undefined behavior |
| 84 | + |
| 85 | + ptr::write_volatile(ptr::null_mut(), v); |
| 86 | + //~^ ERROR calling this function with a null pointer is undefined behavior |
| 87 | + |
| 88 | + ptr::write_bytes::<usize>(ptr::null_mut(), 42, 0); |
| 89 | + //~^ ERROR calling this function with a null pointer is undefined behavior |
| 90 | + |
| 91 | + // with indirections |
| 92 | + let const_ptr = null_ptr as *const u8; |
| 93 | + let _a: u8 = ptr::read(const_ptr); |
| 94 | + //~^ ERROR calling this function with a null pointer is undefined behavior |
| 95 | +} |
| 96 | + |
| 97 | +unsafe fn zst() { |
| 98 | + struct Zst; // zero-sized type |
| 99 | + |
| 100 | + std::slice::from_raw_parts::<()>(ptr::null(), 0); |
| 101 | + //~^ ERROR calling this function with a null pointer is undefined behavior |
| 102 | + std::slice::from_raw_parts::<Zst>(ptr::null(), 0); |
| 103 | + //~^ ERROR calling this function with a null pointer is undefined behavior |
| 104 | + std::slice::from_raw_parts_mut::<()>(ptr::null_mut(), 0); |
| 105 | + //~^ ERROR calling this function with a null pointer is undefined behavior |
| 106 | + std::slice::from_raw_parts_mut::<Zst>(ptr::null_mut(), 0); |
| 107 | + //~^ ERROR calling this function with a null pointer is undefined behavior |
| 108 | + |
| 109 | + ptr::read::<()>(ptr::null()); |
| 110 | + ptr::read::<Zst>(ptr::null()); |
| 111 | + |
| 112 | + ptr::write(ptr::null_mut(), ()); |
| 113 | + ptr::write(ptr::null_mut(), Zst); |
| 114 | + |
| 115 | + ptr::copy(ptr::null::<()>(), ptr::null_mut::<()>(), 1); |
| 116 | + ptr::copy(ptr::null::<Zst>(), ptr::null_mut::<Zst>(), 1); |
| 117 | +} |
| 118 | + |
| 119 | +unsafe fn not_invalid() { |
| 120 | + // Simplified false-positive from std quicksort implementation |
| 121 | + |
| 122 | + let mut a = ptr::null_mut(); |
| 123 | + let mut b = (); |
| 124 | + |
| 125 | + loop { |
| 126 | + if false { |
| 127 | + break; |
| 128 | + } |
| 129 | + |
| 130 | + a = &raw mut b; |
| 131 | + } |
| 132 | + |
| 133 | + ptr::write(a, ()); |
| 134 | +} |
| 135 | + |
| 136 | +fn main() {} |
0 commit comments