Skip to content

Commit

Permalink
fix: check memory boundaries before using slice::from_raw_parts
Browse files Browse the repository at this point in the history
  • Loading branch information
juliusl authored and sanbox-irl committed May 21, 2024
1 parent 80a4683 commit 1d06e2c
Showing 1 changed file with 33 additions and 7 deletions.
40 changes: 33 additions & 7 deletions imgui/src/render/draw_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,12 @@ impl DrawData {
}
#[inline]
pub(crate) unsafe fn cmd_lists(&self) -> &[*const DrawList] {
if self.cmd_lists_count <= 0 || self.cmd_lists.is_null() {
return &[];
}
slice::from_raw_parts(
self.cmd_lists as *const *const DrawList,
self.cmd_lists_count as usize,
self.cmd_lists as *const *const DrawList,
self.cmd_lists_count as usize
)
}
/// Converts all buffers from indexed to non-indexed, in case you cannot render indexed
Expand Down Expand Up @@ -146,23 +149,39 @@ impl RawWrapper for DrawList {
impl DrawList {
#[inline]
pub(crate) unsafe fn cmd_buffer(&self) -> &[sys::ImDrawCmd] {
slice::from_raw_parts(
self.0.CmdBuffer.Data as *const sys::ImDrawCmd,
self.0.CmdBuffer.Size as usize,
)
unsafe {
if self.0.CmdBuffer.Size <= 0 || self.0.CmdBuffer.Data.is_null() {
return &[];
}

slice::from_raw_parts(
self.0.CmdBuffer.Data as *const sys::ImDrawCmd,
self.0.CmdBuffer.Size as usize,
)
}
}

#[inline]
pub fn idx_buffer(&self) -> &[DrawIdx] {
unsafe {
if self.0.IdxBuffer.Size <= 0 || self.0.IdxBuffer.Data.is_null() {
return &[];
}

slice::from_raw_parts(
self.0.IdxBuffer.Data as *const DrawIdx,
self.0.IdxBuffer.Size as usize,
)
}
}

#[inline]
pub fn vtx_buffer(&self) -> &[DrawVert] {
unsafe {
if self.0.VtxBuffer.Size <= 0 || self.0.VtxBuffer.Data.is_null() {
return &[];
}

slice::from_raw_parts(
self.0.VtxBuffer.Data as *const DrawVert,
self.0.VtxBuffer.Size as usize,
Expand All @@ -181,7 +200,14 @@ impl DrawList {
core::mem::size_of::<DrawVert>(),
);
assert!(core::mem::align_of::<VTy>() <= core::mem::align_of::<DrawVert>());
slice::from_raw_parts(self.0.VtxBuffer.Data.cast(), self.0.VtxBuffer.Size as usize)
if self.0.VtxBuffer.Size <= 0 || self.0.VtxBuffer.Data.is_null() {
return &[];
}

slice::from_raw_parts(
self.0.VtxBuffer.Data as *const VTy,
self.0.VtxBuffer.Size as usize,
)
}

#[inline]
Expand Down

0 comments on commit 1d06e2c

Please sign in to comment.