From fcb65574027e204589d77a879f41b2c8d36fc400 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 30 Oct 2026 17:47:06 -0600 Subject: [PATCH] [face] Duplicate the loader environment value instead of borrowing it (#6502) get_default_loader_name() cached the pointer returned by getenv("HB_FACE_LOADER") for the process lifetime. That storage is owned by the C runtime or the caller and may change or be freed after the lookup, leaving the cached pointer dangling. Duplicate the value on first use and hold onto the copy, rather than caching the borrowed pointer or calling getenv() on every face load. Use the existing hb_lazy_loader_t pattern (as static_face_loader_list does): an unset or empty value, or an allocation failure, caches the empty-string default; a non-empty value is copied with hb_malloc and freed via hb_atexit. Add an api smoke test that loads the fixture through the file and blob default-loader entry points (loader == NULL) with HB_FACE_LOADER set, then restores the caller's environment. Its value is LSan coverage of the copy-and-free path in the asan-ubsan job; glibc's setenv() does not free replaced strings, so the borrowed-pointer bug is not observable on Linux. Verified: full meson test suite (275 tests) and test-face under ASan/UBSan with leak detection enabled both pass. Alternative to #6493, following review feedback there. Assisted-by: Claude Opus 4.8 --- src/hb-face.cc | 47 +++++++++++++++++++++++++++++++++----------- test/api/test-face.c | 33 +++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 11 deletions(-) diff --git a/src/hb-face.cc b/src/hb-face.cc index badf5d9c3..9c2840f21 100644 --- a/src/hb-face.cc +++ b/src/hb-face.cc @@ -362,19 +362,44 @@ static const struct supported_face_loaders_t { #endif }; +static inline void free_static_default_loader_name (); + +static struct hb_default_loader_name_lazy_loader_t : hb_lazy_loader_t +{ + static char *create () + { + const char *env = getenv ("HB_FACE_LOADER"); + if (!env || !env[0]) + return nullptr; + + /* Duplicate the value and hold onto it: the environment storage is owned + * by the C runtime or the caller and may change or be freed after this + * lookup. */ + size_t len = strlen (env) + 1; + char *loader_name = (char *) hb_malloc (len); + if (unlikely (!loader_name)) + return nullptr; + hb_memcpy (loader_name, env, len); + + hb_atexit (free_static_default_loader_name); + + return loader_name; + } + static void destroy (char *p) + { hb_free (p); } + static const char *get_null () + { return ""; } +} static_default_loader_name; + +static inline void free_static_default_loader_name () +{ + static_default_loader_name.free_instance (); +} + static const char *get_default_loader_name () { - static hb_atomic_t static_loader_name; - const char *loader_name = static_loader_name.get_acquire (); - if (!loader_name) - { - loader_name = getenv ("HB_FACE_LOADER"); - if (!loader_name) - loader_name = ""; - if (!static_loader_name.cmpexch (nullptr, loader_name)) - loader_name = static_loader_name.get_acquire (); - } - return loader_name; + return static_default_loader_name.get (); } /** diff --git a/test/api/test-face.c b/test/api/test-face.c index 0567c21d3..7d27ce5ab 100644 --- a/test/api/test-face.c +++ b/test/api/test-face.c @@ -75,6 +75,38 @@ test_create_from_blob_using (const void *user_data) hb_face_destroy (face); } +static void +test_default_loader_environment (void) +{ + /* Exercise the HB_FACE_LOADER default-loader path (loader == NULL). The + * value is read from the environment and duplicated once, then held for + * the process lifetime, so loading must succeed and must not depend on the + * environment storage staying valid afterwards. */ + char *saved_loader = g_strdup (g_getenv ("HB_FACE_LOADER")); + g_assert_true (g_setenv ("HB_FACE_LOADER", "ot", TRUE)); + + hb_blob_t *blob = hb_blob_create_from_file_or_fail (font_file); + g_assert_nonnull (blob); + + hb_face_t *face = hb_face_create_from_file_or_fail_using (font_file, face_index, NULL); + g_assert_nonnull (face); + test_face (face); + hb_face_destroy (face); + + face = hb_face_create_or_fail_using (blob, face_index, NULL); + g_assert_nonnull (face); + test_face (face); + hb_face_destroy (face); + + hb_blob_destroy (blob); + + if (saved_loader) + g_assert_true (g_setenv ("HB_FACE_LOADER", saved_loader, TRUE)); + else + g_unsetenv ("HB_FACE_LOADER"); + g_free (saved_loader); +} + int main (int argc, char **argv) { @@ -92,6 +124,7 @@ main (int argc, char **argv) font_file = hb_test_resolve_path (font_file); + hb_test_add (test_default_loader_environment); hb_test_add_flavor ("", test_create_from_file_using); hb_test_add_flavor ("", test_create_from_blob_using); for (const char **loaders = hb_face_list_loaders (); *loaders; loaders++)