From f847bd284db6a38e7f5920421ba23e3aa688f8fd Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Wed, 30 Oct 2024 21:52:16 -0700 Subject: [PATCH] hbwrap: from_raw_parts handling of null ptr use similar approach to 67d4ba9f76470a7ff1f3e7609119cdbb9d33024c closes: https://github.com/wez/wezterm/pull/6337 closes: https://github.com/wez/wezterm/issues/6336 --- wezterm-font/src/hbwrap.rs | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/wezterm-font/src/hbwrap.rs b/wezterm-font/src/hbwrap.rs index 939155972ef..6b9164d3fbc 100644 --- a/wezterm-font/src/hbwrap.rs +++ b/wezterm-font/src/hbwrap.rs @@ -10,10 +10,10 @@ use cairo::Extend; use memmap2::{Mmap, MmapOptions}; use std::ffi::CStr; use std::io::Read; +use std::mem; use std::ops::Range; use std::os::raw::{c_char, c_int, c_uint, c_void}; use std::sync::Arc; -use std::{mem, slice}; use wezterm_color_types::SrgbaPixel; extern "C" { @@ -79,7 +79,7 @@ impl Blob { unsafe { let mut len = 0; let ptr = hb_blob_get_data(self.blob, &mut len); - std::slice::from_raw_parts(ptr as *const u8, len as usize) + from_raw_parts(ptr as *const u8, len as usize) } } @@ -1056,7 +1056,7 @@ impl Buffer { unsafe { let mut len: u32 = 0; let info = hb_buffer_get_glyph_infos(self.buf, &mut len as *mut _); - slice::from_raw_parts(info, len as usize) + from_raw_parts(info, len as usize) } } @@ -1066,7 +1066,7 @@ impl Buffer { unsafe { let mut len: u32 = 0; let pos = hb_buffer_get_glyph_positions(self.buf, &mut len as *mut _); - slice::from_raw_parts(pos, len as usize) + from_raw_parts(pos, len as usize) } } @@ -1180,3 +1180,16 @@ pub fn hb_tag_to_string(tag: hb_tag_t) -> TagString { } TagString(buf) } + +/// Wrapper around std::slice::from_raw_parts that allows for ptr to be +/// null. In the null ptr case, an empty slice is returned. +/// This is necessary because harfbuzz may sometimes encode +/// empty arrays in that way, and rust 1.78 will panic if a null +/// ptr is passed in. +pub(crate) unsafe fn from_raw_parts<'a, T>(ptr: *const T, size: usize) -> &'a [T] { + if ptr.is_null() { + &[] + } else { + std::slice::from_raw_parts(ptr, size) + } +}