diff --git a/chrome/content/zotero/xpcom/pdfWorker/manager.js b/chrome/content/zotero/xpcom/pdfWorker/manager.js index 9e230895be..14dadc16ac 100644 --- a/chrome/content/zotero/xpcom/pdfWorker/manager.js +++ b/chrome/content/zotero/xpcom/pdfWorker/manager.js @@ -271,6 +271,10 @@ class PDFWorker { })); let path = await attachment.getFilePathAsync(); + let fileSize = (await OS.File.stat(path)).size; + if (fileSize > Math.pow(2, 31) - 1) { + throw new Error(`The file "${path}" is too large`); + } let buf = await OS.File.read(path, {}); buf = new Uint8Array(buf).buffer; @@ -341,6 +345,10 @@ class PDFWorker { async processCitaviAnnotations(pdfPath, citaviAnnotations, isPriority, password) { return this._enqueue(async () => { + let fileSize = (await OS.File.stat(pdfPath)).size; + if (fileSize > Math.pow(2, 31) - 1) { + throw new Error(`The file "${pdfPath}" is too large`); + } let buf = await OS.File.read(pdfPath, {}); buf = new Uint8Array(buf).buffer; try { @@ -371,6 +379,10 @@ class PDFWorker { */ async processMendeleyAnnotations(pdfPath, mendeleyAnnotations, isPriority, password) { return this._enqueue(async () => { + let fileSize = (await OS.File.stat(pdfPath)).size; + if (fileSize > Math.pow(2, 31) - 1) { + throw new Error(`The file "${pdfPath}" is too large`); + } let buf = await OS.File.read(pdfPath, {}); buf = new Uint8Array(buf).buffer; try { diff --git a/chrome/content/zotero/xpcom/reader.js b/chrome/content/zotero/xpcom/reader.js index 3730136589..8ca1ccf98b 100644 --- a/chrome/content/zotero/xpcom/reader.js +++ b/chrome/content/zotero/xpcom/reader.js @@ -107,6 +107,14 @@ class ReaderInstance { // Set `ReaderTab` title as fast as possible this.updateTitle(); let path = await item.getFilePathAsync(); + // Check file size, otherwise we get uncatchable error: + // JavaScript error: resource://gre/modules/osfile/osfile_native.jsm, line 60: RangeError: invalid array length + // See more https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Invalid_array_length + let fileSize = (await OS.File.stat(path)).size; + // Max ArrayBuffer size before fx89 is 2GB-1 bytes + if (fileSize > Math.pow(2, 31) - 1) { + throw new Error(`The file "${path}" is too large`); + } let buf = await OS.File.read(path, {}); buf = new Uint8Array(buf).buffer; let annotationItems = item.getAnnotations(); @@ -153,7 +161,9 @@ class ReaderInstance { } uninit() { - this._prefObserverIDs.forEach(id => Zotero.Prefs.unregisterObserver(id)); + if (this._prefObserverIDs) { + this._prefObserverIDs.forEach(id => Zotero.Prefs.unregisterObserver(id)); + } } get itemID() {