Fix font options leak in cairo-surface.c

When calling cairo_surface_get_font_options(), a font options instance
is allocated for the surface. Normally, this just initialised some
otherwise uninitialised fields in cairo_surface_t. Since commit
67eeed44, cairo_font_options_t can contain an extra allocation for a
custom palette. Since commit edf9497c3a, cairo_font_options_t can
contain 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_surface_destroy() finish the contained font
options if they were initialised.

I didn't manage to produce a self-contained test case for this leak. I
found it by just looking at the code. However, I found a way to force a
leak: By adding options->variations=strdtup("slnt=0,wght=400,wdth=100");
to the end of _cairo_font_options_init_default(), all font option
instances now cause a leak unless they are finished. With this extra
change, this commit fixes a memory leak that is simply caused by calling
cairo_surface_get_font_options().

Signed-off-by: Uli Schlachter <psychon@znc.in>
This commit is contained in:
Uli Schlachter 2023-09-17 09:45:04 +02:00
parent 7bf743a92f
commit 4c1987b0f0

View file

@ -983,6 +983,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 */