Skip to content

Don't iterate over all empty slots in the xref entries (bug 1980958) #20170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/core/xref.js
Original file line number Diff line number Diff line change
Expand Up @@ -681,18 +681,23 @@ class XRef {
// When no trailer dictionary candidate exists, try picking the first
// dictionary that contains a /Root entry (fixes issue18986.pdf).
if (!trailerDicts.length) {
for (const [num, entry] of this.entries.entries()) {
if (!entry) {
// In case, this.entries is a sparse array we don't want to
// iterate over empty entries so we use the `in` operator instead of
// using for..of on entries() or a for with the array length.
for (const num in this.entries) {
if (!Object.hasOwn(this.entries, num)) {
continue;
}
const ref = Ref.get(num, entry.gen);
const entry = this.entries[num];
const ref = Ref.get(parseInt(num), entry.gen);
let obj;

try {
obj = this.fetch(ref);
} catch {
continue;
}

if (obj instanceof BaseStream) {
obj = obj.dict;
}
Expand Down
1 change: 1 addition & 0 deletions test/pdfs/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -741,3 +741,4 @@
!print_protection.pdf
!tracemonkey_with_annotations.pdf
!tracemonkey_with_editable_annotations.pdf
!bug1980958.pdf
Binary file added test/pdfs/bug1980958.pdf
Binary file not shown.
14 changes: 14 additions & 0 deletions test/unit/api_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,20 @@ describe("api", function () {

await loadingTask.destroy();
});

it("Doesn't iterate over all empty slots in the xref entries (bug 1980958)", async function () {
if (isNodeJS) {
pending("Worker is not supported in Node.js.");
}
const loadingTask = getDocument(buildGetDocumentParams("bug1980958.pdf"));
const { promise, resolve } = Promise.withResolvers();
setTimeout(() => resolve(null), 1000);

const pdfDocument = await Promise.race([loadingTask.promise, promise]);
expect(pdfDocument?.numPages).toEqual(1);

loadingTask._worker.destroy();
});
});

describe("PDFWorker", function () {
Expand Down
Loading