Merge branch 'font-option-leaks' into 'master'

Fix font options leak in gstate

Closes #795

See merge request cairo/cairo!512
This commit is contained in:
Emmanuele Bassi 2023-09-18 17:40:52 +00:00
commit c45e373fb4
8 changed files with 78 additions and 0 deletions

View file

@ -2173,6 +2173,7 @@ _cairo_ft_scaled_font_fini (void *abstract_font)
if (scaled_font == NULL)
return;
_cairo_font_options_fini (&scaled_font->ft_options.base);
_cairo_unscaled_font_destroy (&scaled_font->unscaled->base);
}

View file

@ -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;

View file

@ -678,6 +678,12 @@ _cairo_scaled_font_init_key (cairo_scaled_font_t *scaled_font,
_cairo_scaled_font_compute_hash (scaled_font);
}
static void
_cairo_scaled_font_fini_key (cairo_scaled_font_t *scaled_font)
{
_cairo_font_options_fini (&scaled_font->options);
}
static cairo_bool_t
_cairo_scaled_font_keys_equal (const void *abstract_key_a,
const void *abstract_key_b)
@ -914,6 +920,7 @@ _cairo_scaled_font_fini_internal (cairo_scaled_font_t *scaled_font)
_cairo_scaled_font_reset_cache (scaled_font);
_cairo_hash_table_destroy (scaled_font->glyphs);
_cairo_font_options_fini (&scaled_font->options);
cairo_font_face_destroy (scaled_font->font_face);
cairo_font_face_destroy (scaled_font->original_font_face);
@ -1105,6 +1112,7 @@ cairo_scaled_font_create (cairo_font_face_t *font_face,
* just wait until it's done, then retry */
_cairo_scaled_font_placeholder_wait_for_creation_to_finish (scaled_font);
}
_cairo_scaled_font_fini_key (&key);
if (scaled_font != NULL) {
/* If the original reference count is 0, then this font must have

View file

@ -2204,6 +2204,7 @@ _cairo_script_surface_finish (void *abstract_surface)
_cairo_pattern_fini (&surface->cr.current_source.base);
_cairo_path_fixed_fini (&surface->cr.current_path);
_cairo_font_options_fini (&surface->cr.current_font_options);
_cairo_surface_clipper_reset (&surface->clipper);
status = cairo_device_acquire (&ctx->base);

View file

@ -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,
@ -983,6 +984,9 @@ cairo_surface_destroy (cairo_surface_t *surface)
if (surface->owns_device)
cairo_device_destroy (surface->device);
if (surface->has_font_options)
_cairo_font_options_fini (&surface->font_options);
assert (surface->snapshot_of == NULL);
assert (! _cairo_surface_has_snapshots (surface));
/* paranoid check that nobody took a reference whilst finishing */

62
test/leaks.c Normal file
View file

@ -0,0 +1,62 @@
/*
* 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 <psychon@znc.in>
*/
#include "cairo-test.h"
// Once upon a time, _cairo_gstate_fini(), _cairo_scaled_font_fini_internal(),
// and _cairo_ft_scaled_font_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)

View file

@ -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',

Binary file not shown.

After

Width:  |  Height:  |  Size: 87 B