Skip to content

Commit

Permalink
Fix load_system_font segfault
Browse files Browse the repository at this point in the history
  • Loading branch information
messense committed Aug 12, 2023
1 parent ddfff8f commit 52a3f7c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
4 changes: 2 additions & 2 deletions mupdf-sys/wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -369,12 +369,12 @@ fz_font *mupdf_new_font(fz_context *ctx, const char *name, int index, mupdf_erro
return font;
}

fz_font *mupdf_new_font_from_memory(fz_context *ctx, const char *name, int index, const unsigned char *data, int data_len, mupdf_error_t **errptr)
fz_font *mupdf_new_font_from_buffer(fz_context *ctx, const char *name, int index, fz_buffer *buffer, mupdf_error_t **errptr)
{
fz_font *font = NULL;
fz_try(ctx)
{
font = fz_new_font_from_memory(ctx, name, data, data_len, index, 0);
font = fz_new_font_from_buffer(ctx, name, buffer, index, 0);
}
fz_catch(ctx)
{
Expand Down
20 changes: 14 additions & 6 deletions src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ impl Buffer {
Ok(Self { inner, offset: 0 })
}

pub fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
let mut buf = Buffer::with_capacity(bytes.len());
buf.write_bytes(bytes)?;
Ok(buf)
}

pub fn with_capacity(cap: usize) -> Self {
let inner = unsafe { fz_new_buffer(context(), cap) };
Self { inner, offset: 0 }
Expand All @@ -48,6 +54,12 @@ impl Buffer {
unsafe { fz_buffer_storage(context(), self.inner, ptr::null_mut()) }
}

pub fn into_inner(mut self) -> *mut fz_buffer {
let inner = self.inner;
self.inner = ptr::null_mut();
inner
}

fn read_bytes(&mut self, buf: &mut [u8]) -> Result<usize, Error> {
let len = buf.len();
let read_len = unsafe {
Expand Down Expand Up @@ -125,19 +137,15 @@ impl TryFrom<&[u8]> for Buffer {
type Error = Error;

fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
let mut buf = Buffer::with_capacity(bytes.len());
buf.write_bytes(bytes)?;
Ok(buf)
Buffer::from_bytes(bytes)
}
}

impl TryFrom<Vec<u8>> for Buffer {
type Error = Error;

fn try_from(bytes: Vec<u8>) -> Result<Self, Self::Error> {
let mut buf = Buffer::with_capacity(bytes.len());
buf.write_bytes(&bytes)?;
Ok(buf)
Buffer::from_bytes(&bytes)
}
}

Expand Down
10 changes: 4 additions & 6 deletions src/font.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use std::ffi::{CStr, CString};
use std::fmt;
use std::os::raw::c_int;
use std::str::FromStr;

use mupdf_sys::*;
use num_enum::TryFromPrimitive;

use crate::{context, Error, Matrix, Path};
use crate::{context, Error, Matrix, Path, Buffer};

#[derive(Debug, Clone, Copy, PartialEq)]
#[repr(C)]
Expand Down Expand Up @@ -75,14 +74,13 @@ impl Font {

pub fn from_bytes_with_index(name: &str, index: i32, font_data: &[u8]) -> Result<Self, Error> {
let c_name = CString::new(name)?;
let data_len = font_data.len() as c_int;
let buffer = Buffer::from_bytes(font_data)?;
let inner = unsafe {
ffi_try!(mupdf_new_font_from_memory(
ffi_try!(mupdf_new_font_from_buffer(
context(),
c_name.as_ptr(),
index,
font_data.as_ptr(),
data_len
buffer.into_inner()
))
};
Ok(Self { inner })
Expand Down

0 comments on commit 52a3f7c

Please sign in to comment.