sparkl
sparkl copied to clipboard
Unsound pub API
src_kernels/gpu_collider.rs
pub struct GpuColliderSet {
pub ptr: *const GpuCollider,
pub len: usize,
}
impl GpuColliderSet {
pub fn get(&self, i: usize) -> Option<&GpuCollider> {
if i >= self.len {
None
} else {
unsafe { Some(&*self.ptr.add(i)) }
}
}
pub fn iter(&self) -> GpuColliderIter {
GpuColliderIter {
ptr: self.ptr,
len: self.len,
_marker: PhantomData,
}
}
}
At function get, the parameter i is checked with a public field self.len, which might be altered without limitation. If self.len cannot reflect the length of the self.ptr, then *self.ptr.add(i) will have memory issues like out-of-bound access. The violated safety requirements is here. In Rust, safe function should not cause any memory issue.
Suggestion:
- mark function with unsafe 2.remove pub from fields
- add necessary checks