Skip to content

Commit e02bd99

Browse files
authored
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
1 parent d19bdbb commit e02bd99

1 file changed

Lines changed: 61 additions & 18 deletions

File tree

src_classic/helper-select.i

Lines changed: 61 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,55 +9,98 @@
99
# maintained and developed by Artifex Software, Inc. https://artifex.com.
1010
# ------------------------------------------------------------------------
1111
*/
12+
13+
1214
void remove_dest_range(fz_context *ctx, pdf_document *pdf, PyObject *numbers)
1315
{
1416
fz_try(ctx) {
15-
int i, j, pno, len, pagecount = pdf_count_pages(ctx, pdf);
17+
int i, j, pno, len;
18+
int pagecount = pdf_count_pages(ctx, pdf);
1619
PyObject *n1 = NULL;
1720
pdf_obj *target, *annots, *pageref, *o, *action, *dest;
21+
1822
for (i = 0; i < pagecount; i++) {
19-
n1 = PyLong_FromLong((long) i);
20-
if (PySet_Contains(numbers, n1)) {
23+
24+
/* Create page index Python int */
25+
n1 = PyLong_FromLong(i);
26+
if (!n1) {
27+
fz_throw(ctx, FZ_ERROR_GENERIC, "Out of memory in PyLong_FromLong");
28+
}
29+
30+
int contains = PySet_Contains(numbers, n1);
31+
if (contains == -1) {
32+
Py_DECREF(n1);
33+
fz_throw(ctx, FZ_ERROR_GENERIC, "PySet_Contains failed");
34+
}
35+
36+
if (contains) {
2137
Py_DECREF(n1);
2238
continue;
2339
}
2440
Py_DECREF(n1);
2541

2642
pageref = pdf_lookup_page_obj(ctx, pdf, i);
2743
annots = pdf_dict_get(ctx, pageref, PDF_NAME(Annots));
28-
if (!annots) continue;
44+
if (!annots) {
45+
continue;
46+
}
47+
2948
len = pdf_array_len(ctx, annots);
30-
for (j = len - 1; j >= 0; j -= 1) {
49+
50+
/* Iterate backwards to safely delete array entries */
51+
for (j = len - 1; j >= 0; j--) {
52+
3153
o = pdf_array_get(ctx, annots, j);
32-
if (!pdf_name_eq(ctx, pdf_dict_get(ctx, o, PDF_NAME(Subtype)), PDF_NAME(Link))) {
54+
55+
if (!pdf_name_eq(ctx,
56+
pdf_dict_get(ctx, o, PDF_NAME(Subtype)),
57+
PDF_NAME(Link))) {
3358
continue;
3459
}
60+
3561
action = pdf_dict_get(ctx, o, PDF_NAME(A));
36-
dest = pdf_dict_get(ctx, o, PDF_NAME(Dest));
62+
dest = pdf_dict_get(ctx, o, PDF_NAME(Dest));
63+
3764
if (action) {
38-
if (!pdf_name_eq(ctx, pdf_dict_get(ctx, action,
39-
PDF_NAME(S)), PDF_NAME(GoTo)))
65+
if (!pdf_name_eq(ctx,
66+
pdf_dict_get(ctx, action, PDF_NAME(S)),
67+
PDF_NAME(GoTo))) {
4068
continue;
69+
}
4170
dest = pdf_dict_get(ctx, action, PDF_NAME(D));
4271
}
72+
4373
pno = -1;
4474
if (pdf_is_array(ctx, dest)) {
4575
target = pdf_array_get(ctx, dest, 0);
4676
pno = pdf_lookup_page_number(ctx, pdf, target);
77+
78+
} else if (pdf_is_string(ctx, dest)) {
79+
fz_location loc = fz_resolve_link(ctx, &pdf->super,
80+
pdf_to_text_string(ctx, dest),
81+
NULL, NULL);
82+
pno = loc.page;
4783
}
48-
else if (pdf_is_string(ctx, dest)) {
49-
fz_location location = fz_resolve_link(ctx, &pdf->super,
50-
pdf_to_text_string(ctx, dest),
51-
NULL, NULL);
52-
pno = location.page;
84+
85+
if (pno < 0) {
86+
continue; /* could not resolve link target */
5387
}
54-
if (pno < 0) { // page number lookup did not work
55-
continue;
88+
89+
n1 = PyLong_FromLong(pno);
90+
if (!n1) {
91+
fz_throw(ctx, FZ_ERROR_GENERIC, "Out of memory in PyLong_FromLong");
92+
}
93+
94+
contains = PySet_Contains(numbers, n1);
95+
if (contains == -1) {
96+
Py_DECREF(n1);
97+
fz_throw(ctx, FZ_ERROR_GENERIC, "PySet_Contains failed");
5698
}
57-
n1 = PyLong_FromLong((long) pno);
58-
if (PySet_Contains(numbers, n1)) {
99+
100+
if (contains) {
59101
pdf_array_delete(ctx, annots, j);
60102
}
103+
61104
Py_DECREF(n1);
62105
}
63106
}

0 commit comments

Comments
 (0)