Avoid UB if the CFData pointer is NULL (#688)

This commit is contained in:
Kornel
2024-07-14 18:50:44 +01:00
committed by GitHub
parent 8002ddd167
commit 3570256ecc
+8 -1
View File
@@ -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.