From 4e8199b572f2035b7749cba276ece3a26630d23e Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 14:18:21 +0200 Subject: Adding upstream version 1.67.1+dfsg1. Signed-off-by: Daniel Baumann --- vendor/rustix/src/utils.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'vendor/rustix/src/utils.rs') diff --git a/vendor/rustix/src/utils.rs b/vendor/rustix/src/utils.rs index efbbe81aa..dcbadb260 100644 --- a/vendor/rustix/src/utils.rs +++ b/vendor/rustix/src/utils.rs @@ -11,3 +11,19 @@ pub(crate) const fn as_ptr(t: &T) -> *const T { pub(crate) fn as_mut_ptr(t: &mut T) -> *mut T { t } + +/// Convert a `*mut c_void` to a `*mut T`, checking that it is not null, +/// misaligned, or pointing to a region of memory that wraps around the address +/// space. +#[allow(dead_code)] +pub(crate) fn check_raw_pointer(value: *mut core::ffi::c_void) -> Option> { + if (value as usize) + .checked_add(core::mem::size_of::()) + .is_none() + || (value as usize) % core::mem::align_of::() != 0 + { + return None; + } + + core::ptr::NonNull::new(value.cast()) +} -- cgit v1.2.3