summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobin Watts <robin.watts@artifex.com>2012-09-03 19:46:47 +0100
committerRobin Watts <robin.watts@artifex.com>2012-09-06 11:05:04 +0100
commit904ad7c43eb23e98dee3fabd09e053dc2702e39d (patch)
treeec0398a93bcb5233d06294cfdeae5890aea82aaf
parent8f1e01856eb96fe30776dfed1644c8f849688d23 (diff)
downloadmupdf-904ad7c43eb23e98dee3fabd09e053dc2702e39d.tar.xz
Add pdf_dict_puts_drop function.
Does the same as pdf_dict_puts, but guarantees to always drop the value passed in (even if the function throws an error). This allows calling code to have a much simpler life in some cases. Update pdf_write to make use of this. Also, fix pdf_dict_puts so it doesn't leak the key object that it creates in the event of an error while growing the dictionary.
-rw-r--r--pdf/mupdf.h1
-rw-r--r--pdf/pdf_object.c42
-rw-r--r--pdf/pdf_write.c18
3 files changed, 44 insertions, 17 deletions
diff --git a/pdf/mupdf.h b/pdf/mupdf.h
index a2fe90f7..b1f3da81 100644
--- a/pdf/mupdf.h
+++ b/pdf/mupdf.h
@@ -76,6 +76,7 @@ pdf_obj *pdf_dict_getp(pdf_obj *dict, char *key);
pdf_obj *pdf_dict_getsa(pdf_obj *dict, char *key, char *abbrev);
void pdf_dict_put(pdf_obj *dict, pdf_obj *key, pdf_obj *val);
void pdf_dict_puts(pdf_obj *dict, char *key, pdf_obj *val);
+void pdf_dict_puts_drop(pdf_obj *dict, char *key, pdf_obj *val);
void pdf_dict_putp(pdf_obj *dict, char *key, pdf_obj *val);
void pdf_dict_del(pdf_obj *dict, pdf_obj *key);
void pdf_dict_dels(pdf_obj *dict, char *key);
diff --git a/pdf/pdf_object.c b/pdf/pdf_object.c
index 3a1e7655..1225a145 100644
--- a/pdf/pdf_object.c
+++ b/pdf/pdf_object.c
@@ -945,9 +945,45 @@ pdf_dict_put(pdf_obj *obj, pdf_obj *key, pdf_obj *val)
void
pdf_dict_puts(pdf_obj *obj, char *key, pdf_obj *val)
{
- pdf_obj *keyobj = pdf_new_name(obj->ctx, key);
- pdf_dict_put(obj, keyobj, val);
- pdf_drop_obj(keyobj);
+ fz_context *ctx = obj->ctx;
+ pdf_obj *keyobj = pdf_new_name(ctx, key);
+
+ fz_try(ctx)
+ {
+ pdf_dict_put(obj, keyobj, val);
+ }
+ fz_always(ctx)
+ {
+ pdf_drop_obj(keyobj);
+ }
+ fz_catch(ctx)
+ {
+ fz_rethrow(ctx);
+ }
+}
+
+void
+pdf_dict_puts_drop(pdf_obj *obj, char *key, pdf_obj *val)
+{
+ fz_context *ctx = obj->ctx;
+ pdf_obj *keyobj = NULL;
+
+ fz_var(keyobj);
+
+ fz_try(ctx)
+ {
+ keyobj = pdf_new_name(ctx, key);
+ pdf_dict_put(obj, keyobj, val);
+ }
+ fz_always(ctx)
+ {
+ pdf_drop_obj(keyobj);
+ pdf_drop_obj(val);
+ }
+ fz_catch(ctx)
+ {
+ fz_rethrow(ctx);
+ }
}
void
diff --git a/pdf/pdf_write.c b/pdf/pdf_write.c
index 4d33770c..d00e154a 100644
--- a/pdf/pdf_write.c
+++ b/pdf/pdf_write.c
@@ -1007,10 +1007,7 @@ add_linearization_objs(pdf_document *xref, pdf_write_options *opts)
opts->rev_renumber_map[params_num] = params_num;
opts->gen_list[params_num] = 0;
opts->rev_gen_list[params_num] = 0;
- o = pdf_new_real(ctx, 1.0);
- pdf_dict_puts(params_obj, "Linearized", o);
- pdf_drop_obj(o);
- o = NULL;
+ pdf_dict_puts_drop(params_obj, "Linearized", pdf_new_real(ctx, 1.0));
opts->linear_l = pdf_new_int(ctx, INT_MIN);
pdf_dict_puts(params_obj, "L", opts->linear_l);
opts->linear_h0 = pdf_new_int(ctx, INT_MIN);
@@ -1018,8 +1015,7 @@ add_linearization_objs(pdf_document *xref, pdf_write_options *opts)
pdf_array_push(o, opts->linear_h0);
opts->linear_h1 = pdf_new_int(ctx, INT_MIN);
pdf_array_push(o, opts->linear_h1);
- pdf_dict_puts(params_obj, "H", o);
- pdf_drop_obj(o);
+ pdf_dict_puts_drop(params_obj, "H", o);
o = NULL;
opts->linear_o = pdf_new_int(ctx, INT_MIN);
pdf_dict_puts(params_obj, "O", opts->linear_o);
@@ -1040,10 +1036,7 @@ add_linearization_objs(pdf_document *xref, pdf_write_options *opts)
opts->rev_renumber_map[hint_num] = hint_num;
opts->gen_list[hint_num] = 0;
opts->rev_gen_list[hint_num] = 0;
- o = pdf_new_int(ctx, 0);
- pdf_dict_puts(hint_obj, "P", o);
- pdf_drop_obj(o);
- o = NULL;
+ pdf_dict_puts_drop(hint_obj, "P", pdf_new_int(ctx, 0));
opts->hints_s = pdf_new_int(ctx, INT_MIN);
pdf_dict_puts(hint_obj, "S", opts->hints_s);
/* FIXME: Do we have thumbnails? Do a T entry */
@@ -1054,10 +1047,7 @@ add_linearization_objs(pdf_document *xref, pdf_write_options *opts)
/* FIXME: Do we have document information? Do an I entry */
/* FIXME: Do we have logical structure heirarchy? Do a C entry */
/* FIXME: Do L, Page Label hint table */
- o = pdf_new_name(ctx, "FlateDecode");
- pdf_dict_puts(hint_obj, "Filter", o);
- pdf_drop_obj(o);
- o = NULL;
+ pdf_dict_puts_drop(hint_obj, "Filter", pdf_new_name(ctx, "FlateDecode"));
opts->hints_length = pdf_new_int(ctx, INT_MIN);
pdf_dict_puts(hint_obj, "Length", opts->hints_length);
xref->table[hint_num].stm_ofs = -1;