From 08f9718a374764d39509277d4a7f94056b1bee4d Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sat, 21 Mar 2020 13:30:56 +0100 Subject: [PATCH] Add a bit more validation in the `ViewHistory` constructor - Ensure that `database.files` actually contains an Array, rather than some arbitrary data. - Only try to lookup an existing entry when the `database` existed on load, since there's obviously nothing to find when `database.files = []` was set (this case is very common in the MOZCENTRAL build since `sessionStorage` is being used there). --- web/view_history.js | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/web/view_history.js b/web/view_history.js index eb37673e1..999a38464 100644 --- a/web/view_history.js +++ b/web/view_history.js @@ -31,19 +31,20 @@ class ViewHistory { this._initializedPromise = this._readFromStorage().then(databaseStr => { const database = JSON.parse(databaseStr || "{}"); - if (!("files" in database)) { + let index = -1; + if (!Array.isArray(database.files)) { database.files = []; } else { while (database.files.length >= this.cacheSize) { database.files.shift(); } - } - let index = -1; - for (let i = 0, length = database.files.length; i < length; i++) { - const branch = database.files[i]; - if (branch.fingerprint === this.fingerprint) { - index = i; - break; + + for (let i = 0, ii = database.files.length; i < ii; i++) { + const branch = database.files[i]; + if (branch.fingerprint === this.fingerprint) { + index = i; + break; + } } } if (index === -1) {