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++)