From e02bd99f54d2c0ecf2e88783c20145b4383ec6db Mon Sep 17 00:00:00 2001 From: Mohataseem Khan Date: Sat, 29 Nov 2025 21:14:39 +0530 Subject: [PATCH] Update helper-select.i 1. added missing Py_DECREF for early-continue branch 2. PyLong_FromLong(i) instead of cast 3. added necessary comments 4.improved readability --- src_classic/helper-select.i | 79 ++++++++++++++++++++++++++++--------- 1 file changed, 61 insertions(+), 18 deletions(-) diff --git a/src_classic/helper-select.i b/src_classic/helper-select.i index 2a547649e..1965e6654 100644 --- a/src_classic/helper-select.i +++ b/src_classic/helper-select.i @@ -9,15 +9,31 @@ # maintained and developed by Artifex Software, Inc. https://artifex.com. # ------------------------------------------------------------------------ */ + + void remove_dest_range(fz_context *ctx, pdf_document *pdf, PyObject *numbers) { fz_try(ctx) { - int i, j, pno, len, pagecount = pdf_count_pages(ctx, pdf); + int i, j, pno, len; + int pagecount = pdf_count_pages(ctx, pdf); PyObject *n1 = NULL; pdf_obj *target, *annots, *pageref, *o, *action, *dest; + for (i = 0; i < pagecount; i++) { - n1 = PyLong_FromLong((long) i); - if (PySet_Contains(numbers, n1)) { + + /* Create page index Python int */ + n1 = PyLong_FromLong(i); + if (!n1) { + fz_throw(ctx, FZ_ERROR_GENERIC, "Out of memory in PyLong_FromLong"); + } + + int contains = PySet_Contains(numbers, n1); + if (contains == -1) { + Py_DECREF(n1); + fz_throw(ctx, FZ_ERROR_GENERIC, "PySet_Contains failed"); + } + + if (contains) { Py_DECREF(n1); continue; } @@ -25,39 +41,66 @@ void remove_dest_range(fz_context *ctx, pdf_document *pdf, PyObject *numbers) pageref = pdf_lookup_page_obj(ctx, pdf, i); annots = pdf_dict_get(ctx, pageref, PDF_NAME(Annots)); - if (!annots) continue; + if (!annots) { + continue; + } + len = pdf_array_len(ctx, annots); - for (j = len - 1; j >= 0; j -= 1) { + + /* Iterate backwards to safely delete array entries */ + for (j = len - 1; j >= 0; j--) { + o = pdf_array_get(ctx, annots, j); - if (!pdf_name_eq(ctx, pdf_dict_get(ctx, o, PDF_NAME(Subtype)), PDF_NAME(Link))) { + + if (!pdf_name_eq(ctx, + pdf_dict_get(ctx, o, PDF_NAME(Subtype)), + PDF_NAME(Link))) { continue; } + action = pdf_dict_get(ctx, o, PDF_NAME(A)); - dest = pdf_dict_get(ctx, o, PDF_NAME(Dest)); + dest = pdf_dict_get(ctx, o, PDF_NAME(Dest)); + if (action) { - if (!pdf_name_eq(ctx, pdf_dict_get(ctx, action, - PDF_NAME(S)), PDF_NAME(GoTo))) + if (!pdf_name_eq(ctx, + pdf_dict_get(ctx, action, PDF_NAME(S)), + PDF_NAME(GoTo))) { continue; + } dest = pdf_dict_get(ctx, action, PDF_NAME(D)); } + pno = -1; if (pdf_is_array(ctx, dest)) { target = pdf_array_get(ctx, dest, 0); pno = pdf_lookup_page_number(ctx, pdf, target); + + } else if (pdf_is_string(ctx, dest)) { + fz_location loc = fz_resolve_link(ctx, &pdf->super, + pdf_to_text_string(ctx, dest), + NULL, NULL); + pno = loc.page; } - else if (pdf_is_string(ctx, dest)) { - fz_location location = fz_resolve_link(ctx, &pdf->super, - pdf_to_text_string(ctx, dest), - NULL, NULL); - pno = location.page; + + if (pno < 0) { + continue; /* could not resolve link target */ } - if (pno < 0) { // page number lookup did not work - continue; + + n1 = PyLong_FromLong(pno); + if (!n1) { + fz_throw(ctx, FZ_ERROR_GENERIC, "Out of memory in PyLong_FromLong"); + } + + contains = PySet_Contains(numbers, n1); + if (contains == -1) { + Py_DECREF(n1); + fz_throw(ctx, FZ_ERROR_GENERIC, "PySet_Contains failed"); } - n1 = PyLong_FromLong((long) pno); - if (PySet_Contains(numbers, n1)) { + + if (contains) { pdf_array_delete(ctx, annots, j); } + Py_DECREF(n1); } }