Skip to content

Commit c17801b

Browse files
committed
Avoid getting null value in RefSet when cloning
1 parent ff1af5a commit c17801b

4 files changed

Lines changed: 24 additions & 5 deletions

File tree

src/core/editor/pdf_editor.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,8 @@ class PDFEditor {
249249
obj = obj.slice();
250250
}
251251
for (let i = 0, ii = obj.length; i < ii; i++) {
252-
const postponedActions = postponedRefCopies.get(obj[i]);
252+
const postponedActions =
253+
obj[i] instanceof Ref && postponedRefCopies.get(obj[i]);
253254
if (postponedActions) {
254255
// The object is a reference that needs to be copied later.
255256
postponedActions.push(ref => (obj[i] = ref));
@@ -277,7 +278,8 @@ class PDFEditor {
277278
}
278279
if (dict) {
279280
for (const [key, rawObj] of dict.getRawEntries()) {
280-
const postponedActions = postponedRefCopies.get(rawObj);
281+
const postponedActions =
282+
rawObj instanceof Ref && postponedRefCopies.get(rawObj);
281283
if (postponedActions) {
282284
// The object is a reference that needs to be copied later.
283285
postponedActions.push(ref => dict.set(key, ref));
@@ -1083,7 +1085,7 @@ class PDFEditor {
10831085

10841086
// Fix the ID tree.
10851087
for (const [id, nodeRef] of idTree || []) {
1086-
const newNodeRef = oldRefMapping.get(nodeRef);
1088+
const newNodeRef = nodeRef instanceof Ref && oldRefMapping.get(nodeRef);
10871089
const newId = dedupIDs.get(id) || id;
10881090
if (newNodeRef) {
10891091
newIdTree.set(newId, newNodeRef);
@@ -1137,7 +1139,7 @@ class PDFEditor {
11371139
const newDestinations = (documentData.destinations = new Map());
11381140
for (const [key, dest] of Object.entries(destinations)) {
11391141
const pageRef = dest[0];
1140-
const pageData = pagesMap.get(pageRef);
1142+
const pageData = pageRef instanceof Ref && pagesMap.get(pageRef);
11411143
if (!pageData) {
11421144
continue;
11431145
}
@@ -1537,7 +1539,7 @@ class PDFEditor {
15371539
}
15381540
const { oldRefMapping } = documentData;
15391541
for (const coRef of co) {
1540-
const newCoRef = oldRefMapping.get(coRef);
1542+
const newCoRef = coRef instanceof Ref && oldRefMapping.get(coRef);
15411543
if (newCoRef) {
15421544
calculationOrder.push(newCoRef);
15431545
}

test/pdfs/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -888,3 +888,4 @@
888888
!outlines_for_editor.pdf
889889
!mesh_shading_empty.pdf
890890
!acroform_calculation_order.pdf
891+
!extractPages_null_in_array.pdf
360 Bytes
Binary file not shown.

test/unit/api_spec.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6763,5 +6763,21 @@ small scripts as well as for`);
67636763
await newLoadingTask.destroy();
67646764
});
67656765
});
6766+
6767+
describe("extract pages with null values in arrays", function () {
6768+
it("should not crash when a page resource contains an array with null entries", async function () {
6769+
const loadingTask = getDocument(
6770+
buildGetDocumentParams("extractPages_null_in_array.pdf")
6771+
);
6772+
const pdfDoc = await loadingTask.promise;
6773+
const data = await pdfDoc.extractPages([{ document: null }]);
6774+
await loadingTask.destroy();
6775+
6776+
const newLoadingTask = getDocument(data);
6777+
const newPdfDoc = await newLoadingTask.promise;
6778+
expect(newPdfDoc.numPages).toEqual(1);
6779+
await newLoadingTask.destroy();
6780+
});
6781+
});
67666782
});
67676783
});

0 commit comments

Comments
 (0)