rusticl/util: make Properties::from_ptr unsafe

Reviewed-by: @LingMan
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/32268>
This commit is contained in:
Karol Herbst 2024-11-27 14:19:44 +01:00 committed by Marge Bot
parent 86453fe053
commit 825936b3f8
4 changed files with 16 additions and 7 deletions

View file

@ -77,7 +77,8 @@ pub fn get_gl_context_info_khr(
let mut gl_context: *mut c_void = ptr::null_mut();
// CL_INVALID_PROPERTY [...] if the same property name is specified more than once.
let props = Properties::from_ptr(properties).ok_or(CL_INVALID_PROPERTY)?;
// SAFETY: properties is a 0 terminated array by spec.
let props = unsafe { Properties::from_ptr(properties) }.ok_or(CL_INVALID_PROPERTY)?;
for (&key, &val) in props.iter() {
match key as u32 {
// CL_INVALID_PLATFORM [...] if platform value specified in properties is not a valid platform.
@ -139,7 +140,8 @@ fn create_context(
let mut gl_context: *mut c_void = ptr::null_mut();
// CL_INVALID_PROPERTY [...] if the same property name is specified more than once.
let props = Properties::from_ptr(properties).ok_or(CL_INVALID_PROPERTY)?;
// SAFETY: properties is a 0 terminated array by spec.
let props = unsafe { Properties::from_ptr(properties) }.ok_or(CL_INVALID_PROPERTY)?;
for (&key, &val) in props.iter() {
match key as u32 {
// CL_INVALID_PLATFORM [...] if platform value specified in properties is not a valid platform.

View file

@ -296,7 +296,7 @@ fn create_buffer_with_properties(
}
// CL_INVALID_PROPERTY [...] if the same property name is specified more than once.
let props = Properties::from_ptr(properties).ok_or(CL_INVALID_PROPERTY)?;
let props = unsafe { Properties::from_ptr(properties) }.ok_or(CL_INVALID_PROPERTY)?;
// CL_INVALID_PROPERTY if a property name in properties is not a supported property name, if
// the value specified for a supported property name is not valid, or if the same property name
@ -801,7 +801,7 @@ fn create_image_with_properties(
.ok_or(CL_IMAGE_FORMAT_NOT_SUPPORTED)?;
// CL_INVALID_PROPERTY [...] if the same property name is specified more than once.
let props = Properties::from_ptr(properties).ok_or(CL_INVALID_PROPERTY)?;
let props = unsafe { Properties::from_ptr(properties) }.ok_or(CL_INVALID_PROPERTY)?;
// CL_INVALID_PROPERTY if a property name in properties is not a supported property name, if
// the value specified for a supported property name is not valid, or if the same property name
@ -1017,8 +1017,9 @@ fn create_sampler_with_properties(
let sampler_properties = if sampler_properties.is_null() {
None
} else {
// SAFETY: sampler_properties is a 0 terminated array by spec.
let sampler_properties =
Properties::from_ptr(sampler_properties).ok_or(CL_INVALID_VALUE)?;
unsafe { Properties::from_ptr(sampler_properties) }.ok_or(CL_INVALID_VALUE)?;
for (&key, &val) in sampler_properties.iter() {
match key as u32 {
CL_SAMPLER_ADDRESSING_MODE => addressing_mode = val as u32,

View file

@ -132,7 +132,8 @@ fn create_command_queue_with_properties(
let properties = if properties.is_null() {
None
} else {
let properties = Properties::from_ptr(properties).ok_or(CL_INVALID_PROPERTY)?;
// SAFETY: properties is a 0 terminated array by spec.
let properties = unsafe { Properties::from_ptr(properties) }.ok_or(CL_INVALID_PROPERTY)?;
for (k, v) in properties.iter() {
match *k as cl_uint {

View file

@ -11,7 +11,12 @@ impl<T> Properties<T> {
///
/// If `p` is null the saved list of properties will be empty. Otherwise it will be 0
/// terminated.
pub fn from_ptr(mut p: *const T) -> Option<Self>
///
/// # Safety
///
/// Besides `p` being valid to be dereferenced, it also needs to point to a `T::default()`
/// terminated array of `T`.
pub unsafe fn from_ptr(mut p: *const T) -> Option<Self>
where
T: Copy + Default + PartialEq,
{