Skip to content

ROB: Recover a corrupt trailing startxref pointer (closes #3238) - #3826

Merged
stefan6419846 merged 4 commits into
py-pdf:mainfrom
binggao1230:fix-3238-recover-corrupt-startxref
Jun 5, 2026
Merged

ROB: Recover a corrupt trailing startxref pointer (closes #3238)#3826
stefan6419846 merged 4 commits into
py-pdf:mainfrom
binggao1230:fix-3238-recover-corrupt-startxref

Conversation

@binggao1230

@binggao1230 binggao1230 commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

Summary

Some PDFs end with a broken trailing cross-reference pointer left behind by a faulty incremental update — the startxref keyword for the final %%EOF has been corrupted (e.g. it reads tartxref), while an earlier, intact startxref from a previous revision is still present further up the file:

trailer
<<...>>
startxref
160113
%%EOF
tartxref      <- corrupt keyword (missing leading 's')
160135
%%EOF

Today PdfReader aborts on such a file with PdfReadError: startxref not found, even though the document is otherwise fully readable from the earlier pointer.

This was noted in #3238, where the suggestion was to "circumvent this by further looking for another startxref". This PR does exactly that: when the startxref keyword expected just above the offset is missing or corrupt, it scans further back for the most recent intact pointer rather than giving up.

Details

  • _find_startxref_pos now delegates to a new _find_previous_startxref_pos fallback when the keyword line is not where it should be.
  • The fallback walks backwards, and because the offset always sits on the line directly below the keyword, it returns the value read immediately before the recovered startxref.
  • The change is purely additive: it only runs when the normal path would have raised, so well-formed files are unaffected. If no earlier pointer exists, the original startxref not found error is still raised.

On the file from #3238 the reader now opens all 11 pages and extracts text correctly, using the valid startxref at 160113.

Test

test_startxref_corrupt_trailing_pointer builds a minimal PDF with a valid startxref/%%EOF followed by a corrupt tartxref trailer and asserts the page is recovered. It runs fully offline (no sample files).

AI use disclosure

Per the project's AI policy: I used AI coding assistance while preparing this PR. Tool: Claude Code. Model: Claude Opus 4.8 (claude-opus-4-8). I've reviewed, run, and verified the change myself and take responsibility for it; the PR text and my replies in this thread are written in my own words.

When the startxref keyword belonging to the final %%EOF is corrupt (for
example a truncated 'tartxref'), scan further back for the most recent
intact startxref from a previous revision instead of raising
'startxref not found'. Closes py-pdf#3238.
@codecov

codecov Bot commented Jun 2, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.76%. Comparing base (52545c5) to head (7dee5c7).
⚠️ Report is 174 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3826      +/-   ##
==========================================
+ Coverage   97.73%   97.76%   +0.02%     
==========================================
  Files          55       55              
  Lines       10417    10457      +40     
  Branches     1931     1943      +12     
==========================================
+ Hits        10181    10223      +42     
+ Misses        130      129       -1     
+ Partials      106      105       -1     

☔ 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.

@stefan6419846 stefan6419846 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for the PR.

Please have a look at the review comments and coverage.

Comment thread pypdf/_reader.py Outdated
Comment thread pypdf/_reader.py Outdated
Comment thread pypdf/_reader.py Outdated
Comment thread pypdf/_reader.py Outdated
Comment thread pypdf/_reader.py
@stefan6419846 stefan6419846 added the needs-test A test should be added before this PR is merged. label Jun 2, 2026
Add direct tests for _find_previous_startxref_pos: same-line keyword,
offset-below-keyword, the unrecoverable cases (keyword without an offset
and no keyword at all) that raise PdfReadError (py-pdf#3238).
@binggao1230

Copy link
Copy Markdown
Contributor Author

Pushed 3d324ba to cover the patch-coverage gap: added direct tests for _find_previous_startxref_pos exercising the same-line keyword form, the offset-below-keyword form, and the two unrecoverable cases (a startxref keyword with no offset above it, and no keyword at all) that raise PdfReadError. All the review threads from earlier should be addressed now — let me know if anything else is needed.

Exercise the _MAX_STARTXREF_RECOVERY_LINES limit: a long stream with no
recoverable pointer gives up rather than scanning forever (py-pdf#3238).
@binggao1230
binggao1230 force-pushed the fix-3238-recover-corrupt-startxref branch from 5f03fb0 to 7dee5c7 Compare June 5, 2026 02:21
@stefan6419846 stefan6419846 removed the needs-test A test should be added before this PR is merged. label Jun 5, 2026
@stefan6419846
stefan6419846 merged commit ddd3485 into py-pdf:main Jun 5, 2026
20 checks passed
stefan6419846 added a commit that referenced this pull request Jun 5, 2026
## What's new

### Security (SEC)
- Avoid infinite loops for outlines and text extraction (#3830) by @stefan6419846

### New Features (ENH)
- Add Japanese predefined CMaps (#3800) by @yasuhiroiwaki
- Font: Collect all character widths, not only those that can be unicode mapped (#3798) by @PJBrs

### Robustness (ROB)
- Recover a corrupt trailing startxref pointer (closes #3238) (#3826) by @gaoflow
- Handle /Pages node without /Kids during flattening (#3825) by @gaoflow
- Accept inline image EI marker at the end of a content stream (#3827) by @gaoflow

### Maintenance (MAINT)
- Type the always-raising deprecation helpers as `NoReturn` (#3819) by @estelledc

[Full Changelog](6.12.2...6.13.0)
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