Skip to content

Unsound pub API #26

@charlesxsh

Description

@charlesxsh

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:

  1. mark function with unsafe
    2.remove pub from fields
  2. add necessary checks

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions