Skip to content

Commit

Permalink
Fix UB in Instance::supports_<prop>()
Browse files Browse the repository at this point in the history
  • Loading branch information
zarik5 committed Sep 15, 2024
1 parent d8ea555 commit 74db542
Showing 1 changed file with 26 additions and 17 deletions.
43 changes: 26 additions & 17 deletions openxr/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,35 +149,44 @@ impl Instance {
}
}

// Note: to avoid undefined behaviour, `ext_props` should be initialized filling in all fields
// manually. The OpenXR spec does not guarantee that `get_system_properties()` will return an
// error when pushing `ext_props` to `SystemProperties::next` if the corresponding extension is
// unsupported. Using `T::out()` and then `assume_init()` could lead to undefined behaviour
// because not all fields may be initialized.
#[inline]
pub fn supports_render_model_loading(&self, system: SystemId) -> Result<bool> {
pub fn get_system_props_ext<T>(&self, system: SystemId, ext_props: &mut T) -> Result<()> {
unsafe {
let mut render_model = sys::SystemRenderModelPropertiesFB::out(ptr::null_mut());
let mut p = sys::SystemProperties::out(&mut render_model as *mut _ as _);
let mut p = sys::SystemProperties::out(ext_props as *mut _ as _);
cvt((self.fp().get_system_properties)(
self.as_raw(),
system,
p.as_mut_ptr(),
))?;
Ok(render_model
.assume_init()
.supports_render_model_loading
.into())
}
Ok(())
}

#[inline]
pub fn supports_render_model_loading(&self, system: SystemId) -> Result<bool> {
let mut props = sys::SystemRenderModelPropertiesFB {
ty: sys::SystemRenderModelPropertiesFB::TYPE,
next: ptr::null_mut(),
supports_render_model_loading: sys::FALSE,
};
self.get_system_props_ext(system, &mut props)?;
Ok(props.supports_render_model_loading.into())
}

#[inline]
pub fn supports_hand_tracking(&self, system: SystemId) -> Result<bool> {
unsafe {
let mut hand = sys::SystemHandTrackingPropertiesEXT::out(ptr::null_mut());
let mut p = sys::SystemProperties::out(&mut hand as *mut _ as _);
cvt((self.fp().get_system_properties)(
self.as_raw(),
system,
p.as_mut_ptr(),
))?;
Ok(hand.assume_init().supports_hand_tracking.into())
}
let mut props = sys::SystemHandTrackingPropertiesEXT {
ty: sys::SystemHandTrackingPropertiesEXT::TYPE,
next: ptr::null_mut(),
supports_hand_tracking: sys::FALSE,
};
self.get_system_props_ext(system, &mut props)?;
Ok(props.supports_hand_tracking.into())
}

/// Construct a `Path` from a string
Expand Down

0 comments on commit 74db542

Please sign in to comment.