Repository navigation
fix: treat generation-mismatched refs as null and key full-save renumbering by (num, gen) - #107
Open
sreyasvpariyath wants to merge 1 commit into
Open
sreyasvpariyath wants to merge 1 commit into
sreyasvpariyath wants to merge 1 commit into
Conversation
…bering by (num, gen)
Contributor
|
@sreyasvpariyath is attempting to deploy a commit to the mythie's projects Team on Vercel. A member of the Team first needs to authorize it. |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
A reference
N G Rcan point to an object whose xref entry and object header both sayN g obj, withg != G. In that caseDocumentParser.getObjectstill returns the live body.The registry then holds two parsed copies for the same object number,
(N,G)and(N,g).writeCompletekeysrenumberMapby object number only, so both copies are given the same output number.The result is that the saved file contains two
M 0 objbodies. The xref points at only one of them, so an edit made through the other path is lost on reload.qpdf --checkstill passes, so the failure is silent.#92 (dangling refs → null on save) does not cover this case: the stale ref does resolve, just to the wrong object. This PR is complementary to #92 and builds on its "unmapped ref → null" behaviour.
Minimal repro (synthetic)
Object 4 lives at generation 1 (xref
00001, header4 1 obj). The page reaches it via4 1 R, while/Destsholds a stale4 0 R.Observed with Node 20.19.2 (main and main + this PR re-checked with Bun 1.4.2 against
src/):["5"]["5"]["5"]["5"][]Spec
Fix
DocumentParser.getObject(src/parser/document-parser.ts):Stale reference N G R: object N is at generation g; treating as null) and resolve to null. The result is cached.writeComplete(src/writer/pdf-writer.ts):renumberRef(s)are keyed by"num gen".(num, gen)is written once.null, as since fix: prevent dangling refs from surviving full-save renumbering #92.Tests
New file
src/writer/stale-generation.test.ts(14 tests):useXRefStreamfalseandtrue:/Dests;/Outlines,/OpenActionand/AcroForm.Results on main @ 37e756a + this PR (Bun 1.4.2,
bun install --frozen-lockfile):New tests without the
src/change: 11 of 14 fail. The 3 negatives pass.New tests with the change: 14 of 14 pass.
vitest run src/writer src/parser: 12 files, 481 tests, all pass.bun run typecheck: passes.bun run lint: 0 errors. The 4 warnings are insrc/signatures/and are identical on unmodified main. Formatting check passes.bun run test:run(full suite): 2 failed / 138 passed files; 6 failed / 3079 passed tests.The 6 failures are the same tests on unmodified main:
lta-finalization.test.ts×5;ltv/gatherer.test.ts"caches revocation data" ×1.They need OCSP/TSA network access, which times out in my environment (
ConnectTimeoutErrorto the TSA host).Notes
pdf-writer.ts, because perf: faster full save after modification #104 replacesrenumberRefswith a numericrefRenumberMap. Whichever lands second needs a small rebase. I'm happy to port the writer half onto perf: faster full save after modification #104 using a(num, gen)key.document-parser.tsandpdf-writer.ts(perf(core): avoid per-ref string allocations via PdfRef.key #62 moves ref keys toPdfRef.key). The(num, gen)keying here maps directly ontoPdfRef.keyif perf(core): avoid per-ref string allocations via PdfRef.key #62 lands first.AI assistance
This change was written with AI assistance, as allowed by CONTRIBUTING.md. Condensed prompts used:
N 0 objbodies. Find the root cause in @libpdf/core and fix it properly, not with a workaround in my app."Review and testing: to be upfront, the code and tests in this PR were written and run by AI coding agents. I am a Java developer and don't read TypeScript fluently, so I have not reviewed every line of the diff myself. I read a plain-language walkthrough of the parser and writer changes and the test results reported above (the new tests fail without the fix and pass with it). The fix has been running in my own app via a patched build. Please review it with that in mind; I'm happy to make changes, and you are very welcome to rework it or treat it as a detailed bug report instead.
Checklist
mainnamedfix/...*.test.ts)bun run typecheckpassesbun run lintpasses (0 errors)Found while building my side project NixRey.