rusticl: verify validity of property names and values

v2: separate from using type aliases based on signature and reorder
v3: verify validity in two additional locations

Reviewed-by: Karol Herbst <kherbst@redhat.com>
Reviewed-by: @LingMan
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/34167>
This commit is contained in:
Seán de Búrca 2025-03-20 15:45:36 -07:00 committed by Marge Bot
parent 62d8541f39
commit 2c202eb787
3 changed files with 17 additions and 9 deletions

View file

@ -80,7 +80,8 @@ pub fn get_gl_context_info_khr(
// SAFETY: properties is a 0 terminated array by spec. // SAFETY: properties is a 0 terminated array by spec.
let props = unsafe { Properties::new(properties) }.ok_or(CL_INVALID_PROPERTY)?; let props = unsafe { Properties::new(properties) }.ok_or(CL_INVALID_PROPERTY)?;
for (&key, &val) in props.iter() { for (&key, &val) in props.iter() {
match key as u32 { let key = u32::try_from(key).or(Err(CL_INVALID_PROPERTY))?;
match key {
// CL_INVALID_PLATFORM [...] if platform value specified in properties is not a valid platform. // CL_INVALID_PLATFORM [...] if platform value specified in properties is not a valid platform.
CL_CONTEXT_PLATFORM => { CL_CONTEXT_PLATFORM => {
(val as cl_platform_id).get_ref()?; (val as cl_platform_id).get_ref()?;
@ -143,7 +144,8 @@ fn create_context(
// SAFETY: properties is a 0 terminated array by spec. // SAFETY: properties is a 0 terminated array by spec.
let props = unsafe { Properties::new(properties) }.ok_or(CL_INVALID_PROPERTY)?; let props = unsafe { Properties::new(properties) }.ok_or(CL_INVALID_PROPERTY)?;
for (&key, &val) in props.iter() { for (&key, &val) in props.iter() {
match key as u32 { let key = u32::try_from(key).or(Err(CL_INVALID_PROPERTY))?;
match key {
// CL_INVALID_PLATFORM [...] if platform value specified in properties is not a valid platform. // CL_INVALID_PLATFORM [...] if platform value specified in properties is not a valid platform.
CL_CONTEXT_PLATFORM => { CL_CONTEXT_PLATFORM => {
(val as cl_platform_id).get_ref()?; (val as cl_platform_id).get_ref()?;

View file

@ -1020,10 +1020,16 @@ fn create_sampler_with_properties(
let sampler_properties = let sampler_properties =
unsafe { Properties::new(sampler_properties) }.ok_or(CL_INVALID_VALUE)?; unsafe { Properties::new(sampler_properties) }.ok_or(CL_INVALID_VALUE)?;
for (&key, &val) in sampler_properties.iter() { for (&key, &val) in sampler_properties.iter() {
match key as u32 { match u32::try_from(key).or(Err(CL_INVALID_VALUE))? {
CL_SAMPLER_ADDRESSING_MODE => addressing_mode = val as u32, CL_SAMPLER_ADDRESSING_MODE => {
CL_SAMPLER_FILTER_MODE => filter_mode = val as u32, addressing_mode = cl_addressing_mode::try_from(val).or(Err(CL_INVALID_VALUE))?
CL_SAMPLER_NORMALIZED_COORDS => normalized_coords = val as u32, }
CL_SAMPLER_FILTER_MODE => {
filter_mode = cl_filter_mode::try_from(val).or(Err(CL_INVALID_VALUE))?
}
CL_SAMPLER_NORMALIZED_COORDS => {
normalized_coords = cl_bool::try_from(val).or(Err(CL_INVALID_VALUE))?
}
// CL_INVALID_VALUE if the property name in sampler_properties is not a supported // CL_INVALID_VALUE if the property name in sampler_properties is not a supported
// property name // property name
_ => return Err(CL_INVALID_VALUE), _ => return Err(CL_INVALID_VALUE),

View file

@ -132,9 +132,9 @@ fn create_command_queue_with_properties(
// SAFETY: properties is a 0 terminated array by spec. // SAFETY: properties is a 0 terminated array by spec.
let properties = unsafe { Properties::new(properties) }.ok_or(CL_INVALID_PROPERTY)?; let properties = unsafe { Properties::new(properties) }.ok_or(CL_INVALID_PROPERTY)?;
for (k, v) in properties.iter() { for (&key, &val) in properties.iter() {
match *k as cl_uint { match u32::try_from(key).or(Err(CL_INVALID_PROPERTY))? {
CL_QUEUE_PROPERTIES => queue_properties = *v, CL_QUEUE_PROPERTIES => queue_properties = val,
// CL_INVALID_QUEUE_PROPERTIES if values specified in properties are valid but are not // CL_INVALID_QUEUE_PROPERTIES if values specified in properties are valid but are not
// supported by the device. // supported by the device.
CL_QUEUE_SIZE => return Err(CL_INVALID_QUEUE_PROPERTIES), CL_QUEUE_SIZE => return Err(CL_INVALID_QUEUE_PROPERTIES),