diff --git a/src/hb-ft.cc b/src/hb-ft.cc index c9e77ad58..67af1621d 100644 --- a/src/hb-ft.cc +++ b/src/hb-ft.cc @@ -1681,7 +1681,10 @@ hb_ft_face_create_from_file_or_fail (const char *file_name, file_name, index, &ft_face))) + { + destroy_ft_library (ft_library); return nullptr; + } FT_Reference_Face (ft_face); hb_face_t *face = hb_ft_face_create (ft_face, _hb_ft_face_destroy_static); @@ -1742,7 +1745,10 @@ hb_ft_face_create_from_blob_or_fail (hb_blob_t *blob, blob_size, index, &ft_face))) + { + destroy_ft_library (ft_library); return nullptr; + } FT_Reference_Face (ft_face); hb_face_t *face = hb_ft_face_create (ft_face, _hb_ft_face_destroy_static); @@ -1836,6 +1842,7 @@ hb_ft_font_set_funcs (hb_font_t *font) &ft_face))) { hb_blob_destroy (blob); + destroy_ft_library (ft_library); DEBUG_MSG (FT, font, "FT_New_Memory_Face() failed"); return; } @@ -1852,6 +1859,7 @@ hb_ft_font_set_funcs (hb_font_t *font) { DEBUG_MSG (FT, font, "hb_blob_set_user_data() failed"); _hb_ft_face_destroy_static (ft_face); + destroy_ft_library (ft_library); return; } diff --git a/test/api/test-ft.c b/test/api/test-ft.c index 5b8053238..2f778efbb 100644 --- a/test/api/test-ft.c +++ b/test/api/test-ft.c @@ -296,6 +296,25 @@ test_native_ft_set_funcs_stream_face_no_deadlock (void) cleanup_freetype (); } +static void +test_native_ft_create_invalid_blob_no_leak (void) +{ + /* The error path of hb_ft_face_create_from_blob_or_fail() must release the + * FT_Library reference it took from reference_ft_library(); otherwise the + * static library is pinned and leaks. Drive the failing path repeatedly so + * LeakSanitizer flags any unbalanced reference. */ + static const char junk[] = "this is not a valid font blob at all"; + + for (unsigned int i = 0; i < 16; i++) + { + hb_blob_t *blob = hb_blob_create (junk, sizeof (junk), + HB_MEMORY_MODE_READONLY, NULL, NULL); + hb_face_t *face = hb_ft_face_create_from_blob_or_fail (blob, 0); + g_assert_null (face); + hb_blob_destroy (blob); + } +} + static gpointer create_static_ft_faces (gpointer data) { @@ -350,6 +369,7 @@ main (int argc, char **argv) hb_test_add (test_native_ft_set_funcs_preserves_load_flags); hb_test_add (test_native_ft_set_funcs_stream_face_no_deadlock); hb_test_add (test_native_ft_glyph_name_zero_size_probe); + hb_test_add (test_native_ft_create_invalid_blob_no_leak); hb_test_add (test_static_ft_library_multithreaded); return hb_test_run ();