#6499 added caches for the vertical advance and origin, but also
changed what the origin is: its x became half the horizontal
advance (as hb-ot does) instead of FreeType's
horiBearingX - vertBearingX, and a glyph FreeType fails to load got
a cached origin of 0 instead of the callback returning false. That
changed shaping output for fonts whose vertical bearings are not
centered (e.g. bitmap-only fonts), and stopped the fallback to
h_origins on load failure.
Keep the caches, but cache both the x and y FreeType computes, the
same way as before #6499, and return false on load failure without
caching anything. Values that don't fit the unsigned cache after
sign normalization are simply not cached.
Testing: meson test -C build passes. test-ft now checks the
batched origins against FreeType's bearings and that a failed load
returns false on repeat queries. hb-shape --font-funcs=ft
--direction=ttb output over NotoSansKR plus 80 shape/api test fonts,
at several sizes, a variation, and negative scale (810 runs) is
identical to the commit before #6499; on main 30 of those differed
(aat-feat, chromacheck-cbdt, chromacheck-sbix).
Follow-up to #6499
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
hb-ft's vertical-origin callback cached a full origin -- x, y, and a
load-failure flag -- in a bespoke 256-entry direct-mapped cache, because
hb_cache_t holds only a single non-negative integer.
Do what hb-ot does instead. The vertical origin's x is half the glyph's
horizontal advance, already cached for horizontal advances, so only y
needs a cache of its own, and y fits hb_cache_t once its sign is
normalized the way hb-ot normalizes the origin. v_origins now fills x
from hb_ft_get_glyph_h_advances() before taking the lock and caches only
y; the custom cache and its origin struct are removed.
This changes the origin's x for fonts whose FreeType vertical bearing
does not center the glyph (eg. a proportional font laid out vertically):
x was horiBearingX - vertBearingX and is now advance/2, which matches
hb-ot. For CJK and other centered glyphs it is unchanged.
A FreeType load failure is no longer turned into a failed origin that
sent the whole run to the caller's fallback. The glyph will not render,
so its y origin is cached as 0 and the rest of the run keeps its real
origins.
Reported by Anthropic's OSS Scanner as ordinary bug ANT-2026-Y24FNCOQ.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
For vertical text hb_ot_position_default() fetches one advance and one
origin per glyph in the buffer. hb-ft registered only single-glyph
callbacks for these, each going to FreeType with no cache, so a glyph
that is expensive for FreeType to load (eg. a deeply nested CFF
charstring) was loaded again for every occurrence in the text: shaping
time grew linearly with text length. Horizontal advances in hb-ft and
both vertical values in hb-ot are already batched and cached.
Register batched callbacks, hb_ft_get_glyph_v_advances() and
hb_ft_get_glyph_v_origins(), mirroring the horizontal advance callback,
and put a per-font cache in front of FreeType in each.
Vertical advances reuse the same hb_cache_t that horizontal advances
use. A vertical advance is non-positive when y_scale is positive (as in
hb-ot, which scales -advance), while hb_cache_t stores only non-negative
values, so the cache holds the advance's magnitude and the sign is
restored on read -- the same trick hb-ot's vertical-advance cache uses.
An origin is two values plus a failure flag, which hb_cache_t cannot
hold, so origins keep a small direct-mapped cache, hb_ft_glyph_cache_t
(256 entries); failed loads are cached too. The batched origins
callback returns false if any glyph failed to load, reproducing the
all-or-nothing result the single-glyph adapter produced for a run.
Both callbacks consult their cache under the ft_font->lock they already
hold. clear_caches() clears all caches wherever the horizontal cache
was cleared, including from hb_ft_font_set_load_flags(), since the load
flags feed both FreeType calls -- which also fixes the horizontal
advance_cache going stale there.
A cache removes the repetition, not the cost of a first load of each
distinct glyph, which is FreeType's own.
Adds test_native_ft_v_metrics_cache and test_native_ft_v_metrics_batched,
checking that repeated and batched queries agree with the single-glyph
API and a fresh font, that scale and load-flag changes are not served
from the cache, and that the advance sign is right at negative y_scale.
Reported by Anthropic's OSS Scanner as ordinary bug ANT-2026-Y24FNCOQ.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
hb_ft_font_set_funcs() assumed the FT_Face it creates is adopted by
hb_font_set_funcs(), but on an immutable font that call only runs the
destroy callback, which FT_Done_Face()s the face. The trailing
_hb_ft_hb_font_changed() then called FT_Set_Char_Size()/FT_Set_Transform()
on freed memory, crashing an application that merely called the setter on
a frozen hb-ft font (e.g. one already passed to
hb_cairo_font_face_create_for_font()).
Add the same early hb_object_is_immutable() return that the sibling setter
hb_ft_font_set_load_flags() and hb_font_set_funcs_using() already have, so
the call fails silently as the object model documents and no
FT_Face/FT_Library reference is created at all. No change for mutable
fonts; no API/ABI change.
Adds test_native_ft_set_funcs_immutable, which reports the
heap-use-after-free under ASan without the fix and passes with it.
Reported by Anthropic's OSS Scanner as ordinary bug ANT-2026-XZWHSHHC.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
The glyph slot belongs to the shared FreeType face. Copy its bitmap
and dimensions before releasing the face lock, then query extents
and invoke the image callback without the lock. Fail if the bitmap
copy cannot be allocated.
Add a bounded shared-font paint regression using the existing emoji
fixture. Check BGRA blob dimensions and reenter the font from the
image callback. The test crashes before the fix and passes after it.
The full Meson suite passes (274 tests).
Reported by Anthropic's OSS Scanner as ANT-2026-ANVKHBVD.
Assisted-by: OpenAI Codex
Keep the memory-backed FT_Face blob on the hb_face_t even when the
OpenType sanitizer discards its copy. Otherwise the destroy callback
can release the FT_Face while the face or font still uses it. Return
the empty object on allocation failure before accessing that pointer.
Keep a blob loader's input bytes on the FT_Face rather than the
hb_face_t, since retained table blobs can outlive the latter. Keep the
library reference on the input blob and release it outside the static
FreeType mutex. Initialize file-loader ownership before transferring
references as well.
Add regressions for destroy-callback timing, referenced font lifetime,
and table blobs retained past the source face. All 293 tests pass,
including all eight FreeType subtests under ASan and UBSan. A separate
sanitized ownership probe passes 154 allocation-failure cases.
Discovered by Anthropic's OSS Scanner, as vulnerabilities
ANT-2026-FC0F75Y2 and ANT-2026-O2E5WAA5.
Assisted-by: OpenAI Codex
reference_ft_library() returns an owned FT_Library reference that the
success paths hand to the FT_Face finalizer or the blob user-data
destructor. Four early returns in hb_ft_face_create_from_file_or_fail,
hb_ft_face_create_from_blob_or_fail and hb_ft_font_set_funcs returned
without releasing it, pinning the static library on invalid input and
OOM. Release it via destroy_ft_library() on each path.
on a stream-backed FT_Face
hb_ft_font_set_funcs() creates its own internal FT_Face from the
font's face blob, and hooks that blob to the FT_Face's generic
finalizer (_release_blob), with the shared static FT_Library hooked
to the blob's user-data destructor (destroy_ft_library).
On hb_font_destroy(), _hb_ft_face_destroy_static() locks
static_ft_library_mutex and calls FT_Done_Face(), which synchronously
runs the finalizer chain: _release_blob() -> hb_blob_destroy(). For a
face created from a custom FreeType stream (FT_OPEN_STREAM), each
hb_face_reference_blob() call synthesizes a brand-new, unshared blob
(via _hb_ft_reference_table()), so this hb_blob_destroy() call drops
it straight to zero, which triggers destroy_ft_library(), which tries
to lock the same non-recursive static_ft_library_mutex again on the
same thread. Self-deadlock.
This didn't reproduce for a file-path or memory-blob face, because
there the blob handed to the internal FT_Face is the same blob shared
by the outer hb_face_t, so the drop inside the lock only decrements
its refcount without reaching zero; the final unref (and
destroy_ft_library() call) happens later, outside the lock, when the
outer face itself is destroyed.
Fix by holding an extra reference on the blob across the locked
FT_Done_Face() call, so it can only reach a zero refcount (and invoke
destroy_ft_library()) after static_ft_library_mutex has been
released. FT_Done_Face() itself remains fully serialized, as intended
by 46644f885d.
Fixes: https://github.com/harfbuzz/harfbuzz/issues/6276
Tests: meson test -C build (260/260 passed); added
test_native_ft_set_funcs_stream_face_no_deadlock to test/api/test-ft.c,
which hangs on unfixed code and passes with the fix. Also verified
against the reporter's original repro.c.
Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
FreeType requires face creation and destruction on a shared
FT_Library to be serialized. Protect the lazily-created static library,
its reference count, and its internally-owned faces with a lazy mutex.
Keep caller-owned FT_Library instances outside the new lock, and add a
multithreaded regression test for blob-backed faces and font funcs.
Tests: meson test -C build (272/272 passed)
Fixes: https://github.com/harfbuzz/harfbuzz/issues/6224
Assisted-by: OpenAI Codex <codex@openai.com>
* [fontations] Fix OOB write on glyph-name callback with size == 0
The Rust `_hb_fontations_glyph_name()` callback computed
`glyph_name.len().min(size as usize - 1)`. When a caller passed
`size == 0`, `size as usize - 1` underflowed a `usize` to
`usize::MAX`, the `min()` clamp collapsed to `glyph_name.len()`,
and the subsequent `copy_nonoverlapping` / trailing NUL wrote
`glyph_name.len() + 1` bytes into a zero-capacity caller buffer.
This is an out-of-bounds write reachable from the public API
whenever the build was configured with `-Dfontations=enabled`
and the application had called `hb_fontations_font_set_funcs()`.
Short-circuit the `size == 0` case before the subtraction.
Return `true` in that case to match the existing post and CFF1
backends (`hb-ot-post-table.hh`, `hb-ot-cff1-table.hh`), which
both early-out with `return true` on `!buf_len` — the glyph
does have a name; we simply had no buffer to write it into.
Regression tests added in `test/api/test-glyph-names.c` cover
the zero-size path on post and CFF1 backends, a sub-font, a
NULL buffer, and `size == 1` / small-buffer / exact-fit cases
so the truncation path does not silently regress.
Fixes: https://github.com/harfbuzz/harfbuzz/issues/5946
Assisted-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* [font] Reject glyph-name lookups with size == 0 at the wrapper
`hb_font_t::get_glyph_name()` previously pre-wrote the trailing
NUL only when `size != 0` but still dispatched to the backend
callback with `size == 0`. Every current glyph-name backend has
to re-validate that argument: `post` and `cff1` short-circuit
with `return true`, the new Rust `fontations` callback now
short-circuits too (prior commit), and the CoreText callback in
`hb-coretext-font.cc` has a structurally similar `(CFIndex)size
- 1` pattern.
Normalize this at the wrapper layer: return `false` immediately
on `size == 0`, before any backend dispatch and before the
`*name = '\0'` write. This makes the zero-size case a single,
auditable no-op for every glyph-name provider (native or Rust,
current or future) and removes the CoreText underflow from the
reachable surface through the same chokepoint.
Observable change: callers that invoked
`hb_font_get_glyph_name(font, gid, buf, 0)` on post/cff1 backed
fonts used to get `true` back; they now get `false`. `size == 0`
queries carry no useful information for the caller, so this is
considered a safe hardening rather than a contract break.
Assisted-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* [font] Preserve glyph-name zero-size probe semantics
Keep `hb_font_get_glyph_name(..., NULL, 0)` as a valid
return-value-only probe instead of rejecting `size == 0` in the
wrapper.
Fix the remaining backend-specific zero-size paths in hb-ft and
hb-coretext-font so they answer the probe without dereferencing the
caller buffer, and update the API tests to assert the preserved
behavior.
Testing: CCACHE_DISABLE=1 meson test -C build test-glyph-names
Testing: CCACHE_DISABLE=1 meson test -C build test-ft
Assisted-by: OpenAI Codex <codex@openai.com>
---------
Co-authored-by: jeffhuang <jeff@docker.xydrsucermoubd24xgo33yhsgd.bx.internal.cloudapp.net>