Wrap unsafe operations in a safe function that validates inputs and enforces invariants before calling unsafe blocks.
pub fn safe_read(ptr: *const i32) -> Option<i32> {
if ptr.is_null() {
return None;
}
unsafe {
Some(*ptr)
}
}
This pattern ensures the pointer is valid before dereferencing it, preventing undefined behavior while keeping the public API safe.