Skip to content

Commit

Permalink
Move run processor initialization inside fz_try() to avoid leak upon …
Browse files Browse the repository at this point in the history
…TRYLATER.

Without this fix if TRYLATER is thrown while looking up RoleMap the
run processor and all its resources leak.
  • Loading branch information
sebras committed Sep 12, 2024
1 parent 336ee9e commit b426d88
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions source/pdf/pdf-op-run.c
Original file line number Diff line number Diff line change
Expand Up @@ -3195,6 +3195,26 @@ pdf_new_run_processor(fz_context *ctx, pdf_document *doc, fz_device *dev, fz_mat
proc->gstate[0].clip_depth = 0;
proc->gstate[0].ctm = ctm;
}

/* We need to save an extra level to allow for level 0 to be the parent gstate level. */
pdf_gsave(ctx, proc);

/* Structure details */
{
pdf_obj *struct_tree_root = pdf_dict_getl(ctx, pdf_trailer(ctx, doc), PDF_NAME(Root), PDF_NAME(StructTreeRoot), NULL);
proc->struct_parent = struct_parent;
proc->role_map = pdf_keep_obj(ctx, pdf_dict_get(ctx, struct_tree_root, PDF_NAME(RoleMap)));

/* Annotations and XObjects can be their own content items. We spot this by
* the struct_parent looking up to be a singular object. */
if (struct_parent != -1 && struct_tree_root)
{
pdf_obj *struct_obj = pdf_lookup_number(ctx, pdf_dict_get(ctx, struct_tree_root, PDF_NAME(ParentTree)), struct_parent);
if (pdf_is_dict(ctx, struct_obj))
send_begin_structure(ctx, proc, struct_obj);
/* We always end structure as required on closedown, so this is safe. */
}
}
}
fz_catch(ctx)
{
Expand All @@ -3203,25 +3223,5 @@ pdf_new_run_processor(fz_context *ctx, pdf_document *doc, fz_device *dev, fz_mat
fz_rethrow(ctx);
}

/* We need to save an extra level to allow for level 0 to be the parent gstate level. */
pdf_gsave(ctx, proc);

/* Structure details */
{
pdf_obj *struct_tree_root = pdf_dict_getl(ctx, pdf_trailer(ctx, doc), PDF_NAME(Root), PDF_NAME(StructTreeRoot), NULL);
proc->struct_parent = struct_parent;
proc->role_map = pdf_keep_obj(ctx, pdf_dict_get(ctx, struct_tree_root, PDF_NAME(RoleMap)));

/* Annotations and XObjects can be their own content items. We spot this by
* the struct_parent looking up to be a singular object. */
if (struct_parent != -1 && struct_tree_root)
{
pdf_obj *struct_obj = pdf_lookup_number(ctx, pdf_dict_get(ctx, struct_tree_root, PDF_NAME(ParentTree)), struct_parent);
if (pdf_is_dict(ctx, struct_obj))
send_begin_structure(ctx, proc, struct_obj);
/* We always end structure as required on closedown, so this is safe. */
}
}

return (pdf_processor*)proc;
}

0 comments on commit b426d88

Please sign in to comment.