Skip to content

Commit

Permalink
Don't crash if Firefox hands us negative bookmark indices [#417]
Browse files Browse the repository at this point in the history
Firefox will sometimes report that bookmarks are at negative indices in
their parent folder, because of internal inconsistencies in the user's
bookmarks DB. Work around this by clamping the index in `setPosition()`
to positive values.  We'll probably position the bookmark incorrectly,
but we at least won't crash.
  • Loading branch information
josh-berry committed Nov 18, 2023
1 parent fb5a2eb commit f7fb218
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
26 changes: 26 additions & 0 deletions src/model/tree.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,5 +320,31 @@ describe("model/tree", () => {
);
},
);

it("deals with gaps in the indexing", () => {
setPosition(nodes.d, {
parent: parents.root,
index: 10,
});
expect(parents.root.children).to.deep.equal(
["a", "b", "c", "e", "f", "d"].map(i => nodes[i]),
);
expect(nodes.d.position?.parent).to.equal(parents.root);
expect(nodes.d.position?.index).to.equal(5);
});

it("deals with negative indexes", () => {
setPosition(nodes.d, {
parent: parents.root,
index: -2,
});
expect(parents.root.children).to.deep.equal(
["d", "a", "b", "c", "e", "f"].map(i => nodes[i]),
);
expect(nodes.d.position?.parent).to.equal(parents.root);
expect(nodes.d.position?.index).to.equal(0);
expect(nodes.a.position?.index).to.equal(1);
expect(nodes.b.position?.index).to.equal(2);
});
});
});
4 changes: 3 additions & 1 deletion src/model/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ export function setPosition<

if (newPosition) {
const newChildren = newPosition.parent.children;
if (newPosition.index > newChildren.length) {
if (newPosition.index < 0) {
newPosition.index = 0;
} else if (newPosition.index > newChildren.length) {
newPosition.index = newChildren.length;
}

Expand Down

0 comments on commit f7fb218

Please sign in to comment.