EmbarkStudios / rust-gpu

🐉 Making Rust a first-class language and ecosystem for GPU shaders 🚧

Home Page:https://shader.rs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Incorrect index validation

atynagano opened this issue · comments

pub unsafe fn load<T>(&self, byte_index: u32) -> T {
if byte_index + mem::size_of::<T>() as u32 > self.data.len() as u32 {
panic!("Index out of range");
}
buffer_load_intrinsic(self.data, byte_index)
}

if byte_index + mem::size_of::<T>() as u32 > (mem::size_of::<u32> * self.data.len()) as u32

Isn't the right-hand side multiplied by 4 because data is &[u32]?

ByteAddressableBuffer requires for the data to be aligned to 4 bytes, so the current condition is fine IMO; the naming could be better (word_index: u32), but the comment above explains it's inherited from HLSL.

Note that buffer_load_intrinsic() ends up doing 4 * byte_index:

let two = self.constant_u32(DUMMY_SP, 2);

So does the left side size_of<T> needs to be divided by 4? Or does size_of returns dword size?

Oh, yeah - I think it should say mem::size_of::<T>() / 4 then 🤔