Skip to content

Commit

Permalink
Avoid UB if the CFData pointer is NULL (#688)
Browse files Browse the repository at this point in the history
  • Loading branch information
kornelski authored Jul 14, 2024
1 parent 8002ddd commit 3570256
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion core-foundation/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,14 @@ impl CFData {
/// read-only.
#[inline]
pub fn bytes(&self) -> &[u8] {
unsafe { slice::from_raw_parts(CFDataGetBytePtr(self.0), self.len() as usize) }
unsafe {
let ptr = CFDataGetBytePtr(self.0);
// Rust slice must never have a NULL pointer
if ptr.is_null() {
return &[];
}
slice::from_raw_parts(ptr, self.len() as usize)
}
}

/// Returns the length of this byte buffer.
Expand Down

0 comments on commit 3570256

Please sign in to comment.