Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

diff --git a/source/pdf/pdf-object.c b/source/pdf/pdf-object.

c
index 39662b00d..ab3c37227 100644
--- a/source/pdf/pdf-object.c
+++ b/source/pdf/pdf-object.c
@@ -3268,50 +3268,48 @@ int pdf_obj_refs(fz_context *ctx, pdf_obj *obj)

/* Convenience functions */

-static pdf_obj *
-pdf_dict_get_inheritable_imp(fz_context *ctx, pdf_obj *node, pdf_obj *key, int
depth, pdf_cycle_list *cycle_up)
-{
- pdf_cycle_list cycle;
- pdf_obj *val = pdf_dict_get(ctx, node, key);
- if (val)
- return val;
- if (pdf_cycle(ctx, &cycle, cycle_up, node))
- fz_throw(ctx, FZ_ERROR_GENERIC, "cycle in tree (parents)");
- if (depth > 100)
- fz_throw(ctx, FZ_ERROR_GENERIC, "too much recursion in tree
(parents)");
- node = pdf_dict_get(ctx, node, PDF_NAME(Parent));
- if (node)
- return pdf_dict_get_inheritable_imp(ctx, node, key, depth + 1, &cycle);
- return NULL;
-}
-
pdf_obj *
pdf_dict_get_inheritable(fz_context *ctx, pdf_obj *node, pdf_obj *key)
{
- return pdf_dict_get_inheritable_imp(ctx, node, key, 0, NULL);
-}
-
-static pdf_obj *
-pdf_dict_getp_inheritable_imp(fz_context *ctx, pdf_obj *node, const char *path,
int depth, pdf_cycle_list *cycle_up)
-{
- pdf_cycle_list cycle;
- pdf_obj *val = pdf_dict_getp(ctx, node, path);
- if (val)
- return val;
- if (pdf_cycle(ctx, &cycle, cycle_up, node))
- fz_throw(ctx, FZ_ERROR_GENERIC, "cycle in tree (parents)");
- if (depth > 100)
- fz_throw(ctx, FZ_ERROR_GENERIC, "too much recursion in tree
(parents)");
- node = pdf_dict_get(ctx, node, PDF_NAME(Parent));
- if (node)
- return pdf_dict_getp_inheritable_imp(ctx, node, path, depth + 1,
&cycle);
+ pdf_obj *slow = node;
+ int halfbeat = -10;
+ do {
+ pdf_obj* val = pdf_dict_get(ctx, node, key);
+ if (val)
+ return val;
+ node = pdf_dict_get(ctx, node, PDF_NAME(Parent));
+ if (node == slow) {
+ fz_throw(ctx, FZ_ERROR_GENERIC, "too much recursion in tree
(parents)");
+ }
+ ++halfbeat;
+ if (halfbeat >=2 && slow) {
+ slow = pdf_dict_get(ctx, slow, PDF_NAME(Parent));
+ halfbeat = 0;
+ }
+ } while (node);
return NULL;
}

pdf_obj *
pdf_dict_getp_inheritable(fz_context *ctx, pdf_obj *node, const char *path)
{
- return pdf_dict_getp_inheritable_imp(ctx, node, path, 0, NULL);
+ pdf_obj* slow = node;
+ int halfbeat = -10;
+ do {
+ pdf_obj* val = pdf_dict_getp(ctx, node, path);
+ if (val)
+ return val;
+ node = pdf_dict_get(ctx, node, PDF_NAME(Parent));
+ if (node == slow) {
+ fz_throw(ctx, FZ_ERROR_GENERIC, "too much recursion in tree
(parents)");
+ }
+ ++halfbeat;
+ if (halfbeat >= 2 && slow) {
+ slow = pdf_dict_get(ctx, slow, PDF_NAME(Parent));
+ halfbeat = 0;
+ }
+ } while (node);
+ return NULL;
}

void pdf_dict_put_bool(fz_context *ctx, pdf_obj *dict, pdf_obj *key, int x)

You might also like