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