fix(native): custom font hook crash

This commit is contained in:
rhunk
2024-07-28 19:43:31 +02:00
parent 8c4e32d614
commit bc9af4105c

View File

@ -1,19 +1,18 @@
use std::{ffi::CStr, fs}; use std::{ffi::CStr, fs};
use nix::libc::{self, c_uint};
use crate::{config, def_hook, dobby_hook_sym}; use crate::{config, def_hook, dobby_hook_sym};
def_hook!( def_hook!(
open_hook, open_hook,
i32, i32,
|path: *const u8, flags: i32| { |path: *const u8, flags: i32, mode: c_uint| {
let mut path = path;
if let Ok(pathname) = CStr::from_ptr(path).to_str() { if let Ok(pathname) = CStr::from_ptr(path).to_str() {
if pathname == "/system/fonts/NotoColorEmoji.ttf" { if pathname == "/system/fonts/NotoColorEmoji.ttf" {
if let Some(font_path) = config::native_config().custom_emoji_font_path { if let Some(font_path) = config::native_config().custom_emoji_font_path {
if fs::metadata(&font_path).is_ok() { if fs::metadata(&font_path).is_ok() {
path = (font_path.to_owned() + "\0").as_ptr(); return libc::openat(libc::AT_FDCWD, font_path.as_ptr() as *const u8, flags, mode);
debug!("open {}", font_path);
} else { } else {
warn!("custom emoji font path does not exist: {}", font_path); warn!("custom emoji font path does not exist: {}", font_path);
} }
@ -21,11 +20,15 @@ def_hook!(
} }
} }
open_hook_original.unwrap()(path, flags) open_hook_original.unwrap()(path, flags, mode)
} }
); );
pub fn init() { pub fn init() {
if config::native_config().custom_emoji_font_path.is_none() {
return;
}
dobby_hook_sym!("libc.so", "open", open_hook); dobby_hook_sym!("libc.so", "open", open_hook);
} }