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,21 +31,22 @@ 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++) {
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) {
index = database.files.push({ fingerprint: this.fingerprint }) - 1;
}