Skip to content

Commit

Permalink
Fix UB in ShaderModule::from_bytes[_with_data] (vulkano-rs#2191)
Browse files Browse the repository at this point in the history
* Fix UB in `ShaderModule::from_bytes[_with_data]`

* Remove magic
  • Loading branch information
marc0246 authored and hakolao committed Feb 20, 2024
1 parent 302220e commit 1661deb
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions vulkano/src/shader/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,13 +203,15 @@ impl ShaderModule {
///
/// # Panics
///
/// - Panics if `bytes` is not aligned to 4.
/// - Panics if the length of `bytes` is not a multiple of 4.
#[inline]
pub unsafe fn from_bytes(
device: Arc<Device>,
bytes: &[u8],
) -> Result<Arc<ShaderModule>, ShaderModuleCreationError> {
assert!((bytes.len() % 4) == 0);
assert!(bytes.as_ptr() as usize % mem::align_of::<u32>() == 0);
assert!(bytes.len() % mem::size_of::<u32>() == 0);

Self::from_words(
device,
Expand Down Expand Up @@ -309,6 +311,7 @@ impl ShaderModule {
///
/// # Panics
///
/// - Panics if `bytes` is not aligned to 4.
/// - Panics if the length of `bytes` is not a multiple of 4.
pub unsafe fn from_bytes_with_data<'a>(
device: Arc<Device>,
Expand All @@ -318,7 +321,8 @@ impl ShaderModule {
spirv_extensions: impl IntoIterator<Item = &'a str>,
entry_points: impl IntoIterator<Item = EntryPointInfo>,
) -> Result<Arc<ShaderModule>, ShaderModuleCreationError> {
assert!((bytes.len() % 4) == 0);
assert!(bytes.as_ptr() as usize % mem::align_of::<u32>() == 0);
assert!(bytes.len() % mem::size_of::<u32>() == 0);

Self::from_words_with_data(
device,
Expand Down

0 comments on commit 1661deb

Please sign in to comment.