Skip to content

Add getPageFromDestination method#701

Merged
MaximPlusov merged 1 commit into
integrationfrom
destinations
Apr 28, 2026
Merged

Add getPageFromDestination method#701
MaximPlusov merged 1 commit into
integrationfrom
destinations

Conversation

@LonelyMidoriya
Copy link
Copy Markdown
Contributor

@LonelyMidoriya LonelyMidoriya commented Apr 28, 2026

Summary by CodeRabbit

  • Bug Fixes
    • Improved handling of PDF annotation destinations for more reliable page navigation in documents with complex reference structures.

@LonelyMidoriya LonelyMidoriya self-assigned this Apr 28, 2026
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 28, 2026

📝 Walkthrough

Walkthrough

A new static helper method getPageFromDestination is added to PDAnnotation.java. This method processes PDF destination objects, handling string, name, dictionary, and array types by performing appropriate lookups or extractions, returning null if required structures are absent.

Changes

Cohort / File(s) Summary
PDF Destination Resolution
src/main/java/org/verapdf/pd/PDAnnotation.java
Added static helper method getPageFromDestination that interprets destination objects across multiple types (string/name lookups via catalog, dictionary key extraction, array indexing); includes StaticResources import addition.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Poem

🐰 A destination helper hops into view,
Resolving PDF paths, old and new,
Strings, names, dicts—all handled with care,
Named trees and catalogs everywhere!
The destination now knows where to go. ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: adding a new static helper method getPageFromDestination to handle PDF destination object interpretation.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch destinations

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/main/java/org/verapdf/pd/PDAnnotation.java`:
- Around line 295-325: The getPageFromDestination method has null-safety and
lookup-order bugs: ensure you null-check the initial destination before
dereferencing its type (avoid calling destination.getType() if
destination==null), guard uses of destination.getKey(key) and subsequent
destination.size()/at(0) against null returns, and adjust resolution order so
that if destination is a COS_DICT whose value for the provided key is a
COS_STRING or COS_NAME you perform the named-destination lookup (using
PDNamesDictionary/PDNameTreeNode or Catalog.getDests()) on that value rather
than skipping it; update logic in getPageFromDestination to reassign destination
only after null-checks and to handle getKey/getString/getDirectBase returning
null before further dereferences (refer to method getPageFromDestination,
variables destination, key, PDNamesDictionary, PDNameTreeNode, and calls
getKey/getString/getDirectBase).
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 98bb62a4-bb35-445f-aa4b-ac3f7cfa4722

📥 Commits

Reviewing files that changed from the base of the PR and between c60d70c and 74a0194.

📒 Files selected for processing (1)
  • src/main/java/org/verapdf/pd/PDAnnotation.java

Comment on lines +295 to +325
public static COSObject getPageFromDestination(COSObject destination, ASAtom key) {
if (destination.getType() == COSObjType.COS_STRING) {
PDNamesDictionary namesDictionary = StaticResources.getDocument().getCatalog().getNamesDictionary();
if (namesDictionary == null) {
return null;
}
PDNameTreeNode dests = namesDictionary.getDests();
if (dests != null) {
destination = dests.getObject(destination.getString());
if (destination == null) {
return null;
}
}
} else if (destination.getType() == COSObjType.COS_NAME) {
COSObject dests = StaticResources.getDocument().getCatalog().getDests();
if (dests != null) {
destination = dests.getKey(destination.getDirectBase().getName());
if (destination == null) {
return null;
}
}
}
if (destination.getType() == COSObjType.COS_DICT) {
destination = destination.getKey(key);
}
COSObject obj = null;
if (destination.getType() == COSObjType.COS_ARRAY && destination.size() > 0) {
obj = destination.at(0);
}
return obj;
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Fix null-safety and destination resolution order in getPageFromDestination.

There are two correctness issues:

  • On Line [295], destination can be null and is dereferenced immediately.
  • On Line [318], destination.getKey(key) can return null, then Line [321] dereferences it.
  • Also, when a dict resolves to COS_STRING/COS_NAME, named-destination lookup is skipped because that logic runs earlier.
Proposed fix
 public static COSObject getPageFromDestination(COSObject destination,  ASAtom key) {
-    if (destination.getType() == COSObjType.COS_STRING) {
-        PDNamesDictionary namesDictionary = StaticResources.getDocument().getCatalog().getNamesDictionary();
-        if (namesDictionary == null) {
-            return null;
-        }
-        PDNameTreeNode dests = namesDictionary.getDests();
-        if (dests != null) {
-            destination = dests.getObject(destination.getString());
-            if (destination == null) {
-                return null;
-            }
-        }
-    } else if (destination.getType() == COSObjType.COS_NAME) {
-        COSObject dests = StaticResources.getDocument().getCatalog().getDests();
-        if (dests != null) {
-            destination = dests.getKey(destination.getDirectBase().getName());
-            if (destination == null) {
-                return null;
-            }
-        }
-    }
-    if (destination.getType() == COSObjType.COS_DICT) {
-        destination = destination.getKey(key);
-    }
-    COSObject obj = null;
-    if (destination.getType() == COSObjType.COS_ARRAY && destination.size() > 0) {
-        obj = destination.at(0);
-    }
-    return obj;
+    if (destination == null) {
+        return null;
+    }
+
+    while (destination != null) {
+        if (destination.getType() == COSObjType.COS_DICT) {
+            destination = destination.getKey(key);
+            continue;
+        }
+        if (destination.getType() == COSObjType.COS_STRING) {
+            PDNamesDictionary namesDictionary = StaticResources.getDocument().getCatalog().getNamesDictionary();
+            if (namesDictionary == null) {
+                return null;
+            }
+            PDNameTreeNode dests = namesDictionary.getDests();
+            destination = dests == null ? null : dests.getObject(destination.getString());
+            continue;
+        }
+        if (destination.getType() == COSObjType.COS_NAME) {
+            COSObject dests = StaticResources.getDocument().getCatalog().getDests();
+            destination = dests == null ? null : dests.getKey(destination.getName());
+            continue;
+        }
+        if (destination.getType() == COSObjType.COS_ARRAY) {
+            return destination.size() > 0 ? destination.at(0) : null;
+        }
+        return null;
+    }
+    return null;
 }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/main/java/org/verapdf/pd/PDAnnotation.java` around lines 295 - 325, The
getPageFromDestination method has null-safety and lookup-order bugs: ensure you
null-check the initial destination before dereferencing its type (avoid calling
destination.getType() if destination==null), guard uses of
destination.getKey(key) and subsequent destination.size()/at(0) against null
returns, and adjust resolution order so that if destination is a COS_DICT whose
value for the provided key is a COS_STRING or COS_NAME you perform the
named-destination lookup (using PDNamesDictionary/PDNameTreeNode or
Catalog.getDests()) on that value rather than skipping it; update logic in
getPageFromDestination to reassign destination only after null-checks and to
handle getKey/getString/getDirectBase returning null before further dereferences
(refer to method getPageFromDestination, variables destination, key,
PDNamesDictionary, PDNameTreeNode, and calls getKey/getString/getDirectBase).

@MaximPlusov MaximPlusov merged commit 2999ba8 into integration Apr 28, 2026
9 checks passed
@MaximPlusov MaximPlusov deleted the destinations branch May 3, 2026 16:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants