Skip to content

Commit

Permalink
hbwrap: from_raw_parts handling of null ptr
Browse files Browse the repository at this point in the history
use similar approach to 67d4ba9

closes: #6337
closes: #6336
  • Loading branch information
wez committed Oct 31, 2024
1 parent 6a0ed04 commit f847bd2
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions wezterm-font/src/hbwrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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)
}
}

Expand All @@ -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)
}
}

Expand Down Expand Up @@ -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)
}
}

0 comments on commit f847bd2

Please sign in to comment.