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).
This commit is contained in:
Jonas Jenwald 2020-03-21 13:30:56 +01:00
parent 3cebb430c2
commit 08f9718a37

View File

@ -31,19 +31,20 @@ class ViewHistory {
this._initializedPromise = this._readFromStorage().then(databaseStr => { this._initializedPromise = this._readFromStorage().then(databaseStr => {
const database = JSON.parse(databaseStr || "{}"); const database = JSON.parse(databaseStr || "{}");
if (!("files" in database)) { let index = -1;
if (!Array.isArray(database.files)) {
database.files = []; database.files = [];
} else { } else {
while (database.files.length >= this.cacheSize) { while (database.files.length >= this.cacheSize) {
database.files.shift(); database.files.shift();
} }
}
let index = -1; for (let i = 0, ii = database.files.length; i < ii; i++) {
for (let i = 0, length = database.files.length; i < length; i++) { const branch = database.files[i];
const branch = database.files[i]; if (branch.fingerprint === this.fingerprint) {
if (branch.fingerprint === this.fingerprint) { index = i;
index = i; break;
break; }
} }
} }
if (index === -1) { if (index === -1) {