Skip to content

Commit 3570256

Browse files
authored
Avoid UB if the CFData pointer is NULL (#688)
1 parent 8002ddd commit 3570256

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

core-foundation/src/data.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,14 @@ impl CFData {
8181
/// read-only.
8282
#[inline]
8383
pub fn bytes(&self) -> &[u8] {
84-
unsafe { slice::from_raw_parts(CFDataGetBytePtr(self.0), self.len() as usize) }
84+
unsafe {
85+
let ptr = CFDataGetBytePtr(self.0);
86+
// Rust slice must never have a NULL pointer
87+
if ptr.is_null() {
88+
return &[];
89+
}
90+
slice::from_raw_parts(ptr, self.len() as usize)
91+
}
8592
}
8693

8794
/// Returns the length of this byte buffer.

0 commit comments

Comments
 (0)