From 9529d02f6aecb234c1e4aaffd972eb439a74fb9a Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Sun, 17 Sep 2023 09:22:29 +0200 Subject: [PATCH] Fix font options leak in gstate cairo_gstate_t contains a cairo_font_options_t. Since commit 67eeed44, this can contain an extra allocation for a custom palette. Since commit edf9497c3a, this contains an extra allocation for a string. Before these commit, font options could just be dropped, but now they need to be freed. This commit makes _cairo_gstate_fini() finish the font options to free the memory allocation. The new test was run via "valgrind --leak-check=full ./cairo-test-suite -f leaks-set-scaled-font". The following reported leak goes away thanks to this commit: 1,040 bytes in 26 blocks are definitely lost in loss record 6 of 12 at 0x48407B4: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so) by 0x4886C62: _cairo_font_options_init_copy (cairo-font-options.c:105) by 0x488C029: _cairo_gstate_set_font_options (cairo-gstate.c:1757) by 0x48841D7: _cairo_default_context_set_scaled_font (cairo-default-context.c:1310) by 0x490809A: cairo_set_scaled_font (cairo.c:3318) by 0x15BF1F: leaks_set_scaled_font (leaks.c:45) by 0x129EF0: cairo_test_for_target (cairo-test.c:938) 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) Fixes: https://gitlab.freedesktop.org/cairo/cairo/-/issues/795 Signed-off-by: Uli Schlachter --- src/cairo-gstate.c | 1 + test/leaks.c | 61 +++++++++++++++++++ test/meson.build | 1 + test/reference/leaks-set-scaled-font.ref.png | Bin 0 -> 87 bytes 4 files changed, 63 insertions(+) create mode 100644 test/leaks.c create mode 100644 test/reference/leaks-set-scaled-font.ref.png diff --git a/src/cairo-gstate.c b/src/cairo-gstate.c index 8a253468d..e9019e8e6 100644 --- a/src/cairo-gstate.c +++ b/src/cairo-gstate.c @@ -193,6 +193,7 @@ void _cairo_gstate_fini (cairo_gstate_t *gstate) { _cairo_stroke_style_fini (&gstate->stroke_style); + _cairo_font_options_fini (&gstate->font_options); cairo_font_face_destroy (gstate->font_face); gstate->font_face = NULL; diff --git a/test/leaks.c b/test/leaks.c new file mode 100644 index 000000000..612d4a1e6 --- /dev/null +++ b/test/leaks.c @@ -0,0 +1,61 @@ +/* + * Copyright © 2023 Uli Schlachter + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, copy, + * modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + * Author: Uli Schlachter + */ + +#include "cairo-test.h" + + +// Once upon a time, _cairo_gstate_fini leaked font options +static cairo_test_status_t +leaks_set_scaled_font (cairo_t *cr, int width, int height) +{ + cairo_font_options_t *opt; + cairo_matrix_t matrix; + cairo_scaled_font_t *font; + + cairo_matrix_init_identity (&matrix); + + opt = cairo_font_options_create (); + cairo_font_options_set_custom_palette_color (opt, 0, 1, 1, 1, 1); + + font = cairo_scaled_font_create (cairo_get_font_face (cr), &matrix, &matrix, opt); + + cairo_set_scaled_font (cr, font); + + cairo_font_options_destroy (opt); + cairo_scaled_font_destroy (font); + + // Fill the output so that the same ref image works for everying + cairo_paint (cr); + + return CAIRO_TEST_SUCCESS; +} + +CAIRO_TEST (leaks_set_scaled_font, + "Regression test for font options memory leak in cairo_set_scaled_font", + "leak", /* keywords */ + NULL, /* requirements */ + 1, 1, + NULL, leaks_set_scaled_font) diff --git a/test/meson.build b/test/meson.build index 1d76d5daa..70f517f3c 100644 --- a/test/meson.build +++ b/test/meson.build @@ -190,6 +190,7 @@ test_sources = [ 'large-source.c', 'large-source-roi.c', 'large-twin-antialias-mixed.c', + 'leaks.c', 'leaky-dash.c', 'leaky-dashed-rectangle.c', 'leaky-dashed-stroke.c', diff --git a/test/reference/leaks-set-scaled-font.ref.png b/test/reference/leaks-set-scaled-font.ref.png new file mode 100644 index 0000000000000000000000000000000000000000..7d5589c1d75ce04efe231a08cabdc9732bcddcb0 GIT binary patch literal 87 zcmeAS@N?(olHy`uVBq!ia0vp^j3CUx1SBVv2j2ryY)RhkE)4%caKYZ?lYt^Uo-U3d e95a&>5)yzs76!(}T%wPGECx?kKbLh*2~7Y6EED1Y literal 0 HcmV?d00001