Commit 5498d93e authored by Michael Vrable's avatar Michael Vrable Committed by Carlos Garcia Campos

Use a single global FT_Library in CairoOutputDev

Cairo may internally keep references to the FreeType fonts loaded in
CairoFontEngine even after poppler is done with them.  Commit 42db4890
("Do not call FT_Done_Face on a live cairo_font_face_t") introduced a fix
for one use-after-free bug, by delaying deleting an FT_Face objects until
cairo is done with it.

That fix doesn't correct all the bugs.  An FT_Library object is created for
each CairoOutputDev object, and deleted when the CairoOutputDev goes away.
But the FT_Library object should not be deleted while fonts loaded using it
are still in use.  And cairo can keep references to fonts around more or
less indefinitely.

To more fully fix the problem, we can either:
 1. Keep a count of not-yet-deleted fonts associated with each FT_Library,
    and wait to call FT_Done_FreeType until it drops to zero.
 2. Never call FT_Done_FreeType.

The second option is the simplest.  To avoid leaking memory FT_Library
objects, use a single global FT_Library instead of a per-CairoOutputDev
copy.
parent c75632d6
......@@ -74,10 +74,23 @@ void CairoImage::setImage (cairo_surface_t *image) {
// CairoOutputDev
//------------------------------------------------------------------------
// We cannot tie the lifetime of an FT_Library object to that of
// CairoOutputDev, since any FT_Faces created with it may end up with a
// reference by Cairo which can be held long after the CairoOutputDev is
// deleted. The simplest way to avoid problems is to never tear down the
// FT_Library instance; to avoid leaks, just use a single global instance
// initialized the first time it is needed.
FT_Library CairoOutputDev::ft_lib;
GBool CairoOutputDev::ft_lib_initialized = gFalse;
CairoOutputDev::CairoOutputDev() {
xref = NULL;
FT_Init_FreeType(&ft_lib);
if (!ft_lib_initialized) {
FT_Init_FreeType(&ft_lib);
ft_lib_initialized = gTrue;
}
fontEngine = NULL;
glyphs = NULL;
fill_pattern = NULL;
......@@ -102,8 +115,7 @@ CairoOutputDev::~CairoOutputDev() {
if (fontEngine) {
delete fontEngine;
}
FT_Done_FreeType(ft_lib);
if (cairo)
cairo_destroy (cairo);
cairo_pattern_destroy (stroke_pattern);
......
......@@ -206,7 +206,9 @@ protected:
XRef *xref; // xref table for current document
FT_Library ft_lib;
static FT_Library ft_lib;
static GBool ft_lib_initialized;
CairoFontEngine *fontEngine;
cairo_t *cairo;
cairo_matrix_t orig_matrix;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment