Skip to content

BUG: Report an out-of-range page number when adding a destination or URI - #4016

Merged
stefan6419846 merged 2 commits into
py-pdf:mainfrom
RavSinghChandan:page-number-out-of-range
Aug 27, 2026
Merged

stefan6419846 merged 2 commits into
py-pdf:mainfrom
RavSinghChandan:page-number-out-of-range

Conversation

@RavSinghChandan

Copy link
Copy Markdown
Contributor

add_named_destination and add_uri index the kids array with the page number directly:

>>> writer.add_named_destination("destination", 99)
IndexError: list index out of range
>>> writer.add_uri(99, "https://example.com", rect)
IndexError: list index out of range

Nothing in the message points at the argument that caused it.

_VirtualList already raises IndexError with the index it was given, so both now report the page number through a shared lookup and keep accepting negative ones.

Reverting the change fails the eight new tests

@codecov

codecov Bot commented Aug 26, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.00%. Comparing base (d85ef0f) to head (eefb423).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4016   +/-   ##
=======================================
  Coverage   98.00%   98.00%           
=======================================
  Files          57       57           
  Lines       11136    11143    +7     
  Branches     2090     2091    +1     
=======================================
+ Hits        10914    10921    +7     
  Misses        124      124           
  Partials       98       98           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@RavSinghChandan
RavSinghChandan force-pushed the page-number-out-of-range branch 2 times, most recently from 75730ad to b7e07da Compare August 26, 2026 11:54
Comment thread pypdf/_writer.py Resolved
add_named_destination and add_uri index the kids array with the page number
directly, so a number past the end surfaced as 'IndexError: list index out of
range' with nothing pointing at the argument that caused it.

_VirtualList already raises IndexError with the index it was given, so both
now report the page number and keep accepting negative ones.
The bounds check computed it twice, which is wasteful for a document with many
pages.
@RavSinghChandan
RavSinghChandan force-pushed the page-number-out-of-range branch from b7e07da to eefb423 Compare August 27, 2026 02:38
@RavSinghChandan

Copy link
Copy Markdown
Contributor Author

Good point, the length is taken once now.

@stefan6419846
stefan6419846 merged commit 0b0721e into py-pdf:main Aug 27, 2026
57 of 76 checks passed
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