From a727aacc2d4feb0c6f5c609e05a0d5611aa97292 Mon Sep 17 00:00:00 2001 From: zeniko Date: Sat, 1 Jun 2013 21:25:24 +0200 Subject: prevent deadlock under memory pressure In multiple places, between acquiring and releasing the FREETYPE lock, exceptions may be thrown which aren't caught in order to properly release the lock. This patch introduces the necessary fz_try/fz_always/ fz_catch invocations to prevent a potential deadlock in these situations. RJW: Also fix another problem pointed out by zeniko. Thanks! --- fitz/base_hash.c | 4 +++- fitz/res_font.c | 31 ++++++++++++++++++++++++++----- 2 files changed, 29 insertions(+), 6 deletions(-) (limited to 'fitz') diff --git a/fitz/base_hash.c b/fitz/base_hash.c index 1da8cac9..22b64ec6 100644 --- a/fitz/base_hash.c +++ b/fitz/base_hash.c @@ -160,7 +160,7 @@ fz_resize_hash(fz_context *ctx, fz_hash_table *table, int newsize) if (table->lock == FZ_LOCK_ALLOC) fz_unlock(ctx, FZ_LOCK_ALLOC); - newents = fz_malloc_array(ctx, newsize, sizeof(fz_hash_entry)); + newents = fz_malloc_array_no_throw(ctx, newsize, sizeof(fz_hash_entry)); if (table->lock == FZ_LOCK_ALLOC) fz_lock(ctx, FZ_LOCK_ALLOC); if (table->lock >= 0) @@ -176,6 +176,8 @@ fz_resize_hash(fz_context *ctx, fz_hash_table *table, int newsize) return; } } + if (newents == NULL) + fz_throw(ctx, "hash table resize failed; out of memory (%d entries)", newsize); table->ents = newents; memset(table->ents, 0, sizeof(fz_hash_entry) * newsize); table->size = newsize; diff --git a/fitz/res_font.c b/fitz/res_font.c index 8fb51377..3263f88c 100644 --- a/fitz/res_font.c +++ b/fitz/res_font.c @@ -515,8 +515,19 @@ retry_unhinted: return NULL; } - result = fz_copy_ft_bitmap(ctx, face->glyph->bitmap_left, face->glyph->bitmap_top, &face->glyph->bitmap); - fz_unlock(ctx, FZ_LOCK_FREETYPE); + fz_try(ctx) + { + result = fz_copy_ft_bitmap(ctx, face->glyph->bitmap_left, face->glyph->bitmap_top, &face->glyph->bitmap); + } + fz_always(ctx) + { + fz_unlock(ctx, FZ_LOCK_FREETYPE); + } + fz_catch(ctx) + { + fz_rethrow(ctx); + } + return result; } @@ -623,9 +634,19 @@ fz_render_ft_stroked_glyph(fz_context *ctx, fz_font *font, int gid, const fz_mat } bitmap = (FT_BitmapGlyph)glyph; - pixmap = fz_copy_ft_bitmap(ctx, bitmap->left, bitmap->top, &bitmap->bitmap); - FT_Done_Glyph(glyph); - fz_unlock(ctx, FZ_LOCK_FREETYPE); + fz_try(ctx) + { + pixmap = fz_copy_ft_bitmap(ctx, bitmap->left, bitmap->top, &bitmap->bitmap); + } + fz_always(ctx) + { + FT_Done_Glyph(glyph); + fz_unlock(ctx, FZ_LOCK_FREETYPE); + } + fz_catch(ctx) + { + fz_rethrow(ctx); + } return pixmap; } -- cgit v1.2.3