Skip to content

Commit

Permalink
Merge pull request #12375 from Snuffleupagus/emptyDict-set
Browse files Browse the repository at this point in the history
Ensure that the empty dictionary won't be accidentally modified, and slightly improve the "SaveDocument" handler in `src/core/worker.js`
  • Loading branch information
timvandermeij authored Sep 15, 2020
2 parents e73354a + ed4e7cd commit 374aad7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
9 changes: 8 additions & 1 deletion src/core/primitives.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,14 @@ var Dict = (function DictClosure() {
},
};

Dict.empty = new Dict(null);
Dict.empty = (function () {
const emptyDict = new Dict(null);

emptyDict.set = (key, value) => {
unreachable("Should not call `set` on the empty dictionary.");
};
return emptyDict;
})();

Dict.merge = function ({ xref, dictArray, mergeSubDicts = false }) {
const mergedDict = new Dict(xref);
Expand Down
7 changes: 3 additions & 4 deletions src/core/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
VerbosityLevel,
warn,
} from "../shared/util.js";
import { clearPrimitiveCaches, Dict, isDict, Ref } from "./primitives.js";
import { clearPrimitiveCaches, Dict, Ref } from "./primitives.js";
import { LocalPdfManager, NetworkPdfManager } from "./pdf_manager.js";
import { incrementalUpdate } from "./writer.js";
import { isNodeJS } from "../shared/is_node.js";
Expand Down Expand Up @@ -548,8 +548,7 @@ class WorkerMessageHandler {
return stream.bytes;
}

acroForm = isDict(acroForm) ? acroForm : Dict.empty;
const xfa = acroForm.get("XFA") || [];
const xfa = (acroForm instanceof Dict && acroForm.get("XFA")) || [];
let xfaDatasets = null;
if (Array.isArray(xfa)) {
for (let i = 0, ii = xfa.length; i < ii; i += 2) {
Expand All @@ -568,7 +567,7 @@ class WorkerMessageHandler {
// Get string info from Info in order to compute fileId
const _info = Object.create(null);
const xrefInfo = xref.trailer.get("Info") || null;
if (xrefInfo) {
if (xrefInfo instanceof Dict) {
xrefInfo.forEach((key, value) => {
if (isString(key) && isString(value)) {
_info[key] = stringToPDFString(value);
Expand Down

0 comments on commit 374aad7

Please sign in to comment.