Add getPageFromDestination method#701
Conversation
📝 WalkthroughWalkthroughA new static helper method Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
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
📒 Files selected for processing (1)
src/main/java/org/verapdf/pd/PDAnnotation.java
| 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; | ||
| } |
There was a problem hiding this comment.
Fix null-safety and destination resolution order in getPageFromDestination.
There are two correctness issues:
- On Line [295],
destinationcan 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).
Summary by CodeRabbit