From a5c17f3abe86dbce26bf56be16364ad27e0543a5 Mon Sep 17 00:00:00 2001 From: Martynas Bagdonas Date: Thu, 6 Oct 2022 10:57:04 +0300 Subject: [PATCH] Properly handle errors when trying to open too large (>2GB-1) PDF file (cherry picked from commit e9c6c76e7914e501067a2280e202045bc2c0b2ef) --- chrome/content/zotero/xpcom/pdfWorker/manager.js | 12 ++++++++++++ chrome/content/zotero/xpcom/reader.js | 12 +++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) 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() {