Properly handle errors when trying to open too large (>2GB-1) PDF file

This commit is contained in:
Martynas Bagdonas 2022-10-06 10:57:04 +03:00
parent f13966894f
commit e9c6c76e79
2 changed files with 23 additions and 1 deletions

View file

@ -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 {

View file

@ -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() {