diff options
author | Robin Watts <robin.watts@artifex.com> | 2011-12-15 19:49:16 +0000 |
---|---|---|
committer | Robin Watts <robin.watts@artifex.com> | 2011-12-15 19:49:16 +0000 |
commit | e6118ac1b13cc49f637861fccbc32a10c4ea1ea7 (patch) | |
tree | 4ccb1fb5adf96d7794872502ee975825f250054a | |
parent | 3031a2480fe775df825de6674495d01ae2607d93 (diff) | |
download | mupdf-e6118ac1b13cc49f637861fccbc32a10c4ea1ea7.tar.xz |
Various Memsqueezing fixes.
Fixes for leaks (and SEGVs, division by zeros etc) seen when
Memsqueezing.
-rw-r--r-- | apps/pdfdraw.c | 4 | ||||
-rw-r--r-- | draw/draw_glyph.c | 13 | ||||
-rw-r--r-- | fitz/base_hash.c | 12 | ||||
-rw-r--r-- | fitz/base_object.c | 20 | ||||
-rw-r--r-- | fitz/memento.c | 2 | ||||
-rw-r--r-- | fitz/res_store.c | 10 | ||||
-rw-r--r-- | fitz/stm_open.c | 10 | ||||
-rw-r--r-- | pdf/pdf_parse.c | 2 | ||||
-rw-r--r-- | pdf/pdf_xref.c | 4 |
9 files changed, 66 insertions, 11 deletions
diff --git a/apps/pdfdraw.c b/apps/pdfdraw.c index 1e71181e..fe93352c 100644 --- a/apps/pdfdraw.c +++ b/apps/pdfdraw.c @@ -369,8 +369,6 @@ int main(int argc, char **argv) fz_set_aa_level(ctx, alphabits); - glyphcache = fz_new_glyph_cache(ctx); - colorspace = fz_device_rgb; if (grayscale) colorspace = fz_device_gray; @@ -393,6 +391,8 @@ int main(int argc, char **argv) fz_try(ctx) { + glyphcache = fz_new_glyph_cache(ctx); + while (fz_optind < argc) { filename = argv[fz_optind++]; diff --git a/draw/draw_glyph.c b/draw/draw_glyph.c index 6cb1518a..30f18a5f 100644 --- a/draw/draw_glyph.c +++ b/draw/draw_glyph.c @@ -27,7 +27,15 @@ fz_new_glyph_cache(fz_context *ctx) fz_glyph_cache *cache; cache = fz_malloc(ctx, sizeof(fz_glyph_cache)); - cache->hash = fz_new_hash_table(ctx, 509, sizeof(fz_glyph_key)); + fz_try(ctx) + { + cache->hash = fz_new_hash_table(ctx, 509, sizeof(fz_glyph_key)); + } + fz_catch(ctx) + { + fz_free(ctx, cache); + fz_rethrow(ctx); + } cache->total = 0; return cache; @@ -58,6 +66,9 @@ fz_evict_glyph_cache(fz_context *ctx, fz_glyph_cache *cache) void fz_free_glyph_cache(fz_context *ctx, fz_glyph_cache *cache) { + if (!cache) + return; + fz_evict_glyph_cache(ctx, cache); fz_free_hash(cache->hash); fz_free(ctx, cache); diff --git a/fitz/base_hash.c b/fitz/base_hash.c index d6ff8ca0..79943a1d 100644 --- a/fitz/base_hash.c +++ b/fitz/base_hash.c @@ -54,8 +54,16 @@ fz_new_hash_table(fz_context *ctx, int initialsize, int keylen) table->keylen = keylen; table->size = initialsize; table->load = 0; - table->ents = fz_malloc_array(ctx, table->size, sizeof(fz_hash_entry)); - memset(table->ents, 0, sizeof(fz_hash_entry) * table->size); + fz_try(ctx) + { + table->ents = fz_malloc_array(ctx, table->size, sizeof(fz_hash_entry)); + memset(table->ents, 0, sizeof(fz_hash_entry) * table->size); + } + fz_catch(ctx) + { + fz_free(ctx, table); + fz_rethrow(ctx); + } return table; } diff --git a/fitz/base_object.c b/fitz/base_object.c index 957396ea..ced40414 100644 --- a/fitz/base_object.c +++ b/fitz/base_object.c @@ -389,7 +389,15 @@ fz_new_array(fz_context *ctx, int initialcap) obj->u.a.len = 0; obj->u.a.cap = initialcap > 1 ? initialcap : 6; - obj->u.a.items = fz_malloc_array(ctx, obj->u.a.cap, sizeof(fz_obj*)); + fz_try(ctx) + { + obj->u.a.items = fz_malloc_array(ctx, obj->u.a.cap, sizeof(fz_obj*)); + } + fz_catch(ctx) + { + fz_free(ctx, obj); + fz_rethrow(ctx); + } for (i = 0; i < obj->u.a.cap; i++) obj->u.a.items[i] = NULL; @@ -537,7 +545,15 @@ fz_new_dict(fz_context *ctx, int initialcap) obj->u.d.len = 0; obj->u.d.cap = initialcap > 1 ? initialcap : 10; - obj->u.d.items = fz_malloc_array(ctx, obj->u.d.cap, sizeof(struct keyval)); + fz_try(ctx) + { + obj->u.d.items = fz_malloc_array(ctx, obj->u.d.cap, sizeof(struct keyval)); + } + fz_catch(ctx) + { + fz_free(ctx, obj); + fz_rethrow(ctx); + } for (i = 0; i < obj->u.d.cap; i++) { obj->u.d.items[i].k = NULL; diff --git a/fitz/memento.c b/fitz/memento.c index cb5b59d1..73963f2d 100644 --- a/fitz/memento.c +++ b/fitz/memento.c @@ -446,7 +446,7 @@ static void Memento_endStats(void) fprintf(stderr, "%d mallocs, %d frees, %d reallocs\n", globals.numMallocs, globals.numFrees, globals.numReallocs); fprintf(stderr, "Average allocation size %d bytes\n", - globals.totalAlloc/globals.numMallocs); + (globals.numMallocs != 0 ? globals.totalAlloc/globals.numMallocs: 0)); } void Memento_stats(void) diff --git a/fitz/res_store.c b/fitz/res_store.c index 0b303982..a76a9bb1 100644 --- a/fitz/res_store.c +++ b/fitz/res_store.c @@ -39,7 +39,15 @@ fz_new_store_context(fz_context *ctx, unsigned int max) { fz_store *store; store = fz_malloc(ctx, sizeof(fz_store)); - store->hash = fz_new_hash_table(ctx, 4096, sizeof(struct refkey)); + fz_try(ctx) + { + store->hash = fz_new_hash_table(ctx, 4096, sizeof(struct refkey)); + } + fz_catch(ctx) + { + fz_free(ctx, store); + fz_rethrow(ctx); + } store->head = NULL; store->tail = NULL; store->size = 0; diff --git a/fitz/stm_open.c b/fitz/stm_open.c index eade9788..733b4c6a 100644 --- a/fitz/stm_open.c +++ b/fitz/stm_open.c @@ -89,7 +89,15 @@ fz_open_fd(fz_context *ctx, int fd) state = fz_malloc(ctx, sizeof(int)); *state = fd; - stm = fz_new_stream(ctx, state, read_file, close_file); + fz_try(ctx) + { + stm = fz_new_stream(ctx, state, read_file, close_file); + } + fz_catch(ctx) + { + fz_free(ctx, state); + fz_rethrow(ctx); + } stm->seek = seek_file; return stm; diff --git a/pdf/pdf_parse.c b/pdf/pdf_parse.c index 7174c23a..57713b21 100644 --- a/pdf/pdf_parse.c +++ b/pdf/pdf_parse.c @@ -363,7 +363,9 @@ pdf_parse_dict(pdf_xref *xref, fz_stream *file, char *buf, int cap) val = fz_new_int(ctx, a); fz_dict_put(dict, key, val); fz_drop_obj(val); + val = NULL; fz_drop_obj(key); + key = NULL; goto skip; } if (tok == PDF_TOK_INT) diff --git a/pdf/pdf_xref.c b/pdf/pdf_xref.c index b560d868..d47524b6 100644 --- a/pdf/pdf_xref.c +++ b/pdf/pdf_xref.c @@ -676,7 +676,6 @@ pdf_open_xref_with_stream(fz_stream *file, char *password) xref->trailer = NULL; } fz_warn(xref->ctx, "trying to repair broken xref"); - pdf_repair_xref(xref, xref->scratch, sizeof xref->scratch); repaired = 1; } @@ -684,6 +683,9 @@ pdf_open_xref_with_stream(fz_stream *file, char *password) { int hasroot, hasinfo; + if (repaired) + pdf_repair_xref(xref, xref->scratch, sizeof xref->scratch); + encrypt = fz_dict_gets(xref->trailer, "Encrypt"); id = fz_dict_gets(xref->trailer, "ID"); if (fz_is_dict(encrypt)) |