From 29087868cd28fc95ced901f2f6b35a9ca27b615a Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Sun, 17 Sep 2023 09:51:51 +0200 Subject: [PATCH] Fix font options leak in _cairo_surface_copy_similar_properties() I added options->variations = strdup("slnt=0,wght=400,wdth=100"); to the end of _cairo_font_options_init_default(). This makes all font option objects own some memory that needs to be freed. Then I ran some random test under valgrind and found memory leaks. _cairo_surface_copy_similar_properties() gets the font options of a surface via cairo_surface_get_font_options(). This creates a copy of the font variations that I added above. _cairo_surface_set_font_options() then copies this again (it calls _cairo_font_options_init_copy). Thus, the original copy is still owned by _cairo_surface_copy_similar_properties() and needs to be freed. This commit fixes four leaks in "valgrind --leak-check=full ./cairo-test-suite -f leaks-set-scaled-font". A random example is: 25 bytes in 1 blocks are definitely lost in loss record 4 of 25 at 0x48407B4: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so) by 0x4ECBC99: strdup (strdup.c:42) by 0x4886C0C: _cairo_font_options_init_copy (cairo-font-options.c:99) by 0x48F1DDE: cairo_surface_get_font_options (cairo-surface.c:1620) by 0x48F0691: _cairo_surface_copy_similar_properties (cairo-surface.c:454) by 0x48F087C: cairo_surface_create_similar (cairo-surface.c:528) by 0x1B168A: _cairo_boilerplate_pdf_create_surface (cairo-boilerplate-pdf.c:92) by 0x129B7F: cairo_test_for_target (cairo-test.c:824) by 0x12B37F: _cairo_test_context_run_for_target (cairo-test.c:1545) by 0x12C385: _cairo_test_runner_draw (cairo-test-runner.c:258) by 0x12DEB5: main (cairo-test-runner.c:962) Signed-off-by: Uli Schlachter --- src/cairo-surface.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/cairo-surface.c b/src/cairo-surface.c index 155f43882..c208f99f2 100644 --- a/src/cairo-surface.c +++ b/src/cairo-surface.c @@ -453,6 +453,7 @@ _cairo_surface_copy_similar_properties (cairo_surface_t *surface, cairo_surface_get_font_options (other, &options); _cairo_surface_set_font_options (surface, &options); + _cairo_font_options_fini (&options); } cairo_surface_set_fallback_resolution (surface,