You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Fix SIGBUS crash on macOS arm64 for any \setmainfont{} call
Tectonic 0.16.x crashes with SIGBUS (exit 138) on macOS arm64 on any `\setmainfont{}` call. This PR fixes the root cause and several additional bugs introduced in the `xetex_layout` Rust port (#1138).
ROOT CAUSE
`CFArray`'s `Index` implementation (`crates/mac_core/src/array.rs`) was unsound. `CFArrayGetValueAtIndex` returns the stored `CFTypeRef` value directly (i.e., the pointer *is* the value), but the code treated it as a *pointer to* `T` and dereferenced it:
```rust
let ptr = CFArrayGetValueAtIndex(...).cast::<T>();
unsafe { &*ptr } // reads CF object internals as a NonNull — garbage
```
When `find_font_with_name` called `matches[0].clone()`, this garbage pointer was passed to `CFRetain`, crashing immediately with `EXC_BAD_ACCESS` in `CFRetain`.
FIX
Replace the broken `Index<usize>` impl with a `get(index) -> T` method that correctly calls `CFRetain` via `new_borrowed` on the returned `CFTypeRef`.
ADDITIONAL FIXES
While investigating this issue, I stumbled upon other bugs which I
initially thought were causing the problem here with `\setmainfont{}`
but weren't. I fixed them anyway:
- `c_api.rs`: `Fixed` type on macOS defined as `u32` instead of `i32`, mismatching the C `SInt32` typedef. Negative `scaled_size` values (used by TeX for font probing) became ~65K pt sizes. Changed to `i32`.
- `font.rs:277`: AFM file reading used the already-closed main font `handle` instead of `afm_handle`. Changed to `afm_handle`.
- `font.rs` (macOS init): Several `.unwrap()` calls could panic across `extern "C"` boundaries (undefined behavior). Replaced with `ok_or(())?` / `.map_err()` error propagation.
- `mac_core/font.rs`: `CTFont::new_descriptor` unconditionally unwrapped a potentially-null CoreText return (the original C++ code checked for null). Changed return type to `Option<CTFont>`.
0 commit comments