From 3570256eccb0cc4945f7d5fee08d1d14df865bf6 Mon Sep 17 00:00:00 2001 From: Kornel Date: Sun, 14 Jul 2024 18:50:44 +0100 Subject: [PATCH] Avoid UB if the CFData pointer is NULL (#688) --- core-foundation/src/data.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/core-foundation/src/data.rs b/core-foundation/src/data.rs index 9f95219..b799ed3 100644 --- a/core-foundation/src/data.rs +++ b/core-foundation/src/data.rs @@ -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.