mirror of
https://github.com/outline/outline.git
synced 2026-11-28 16:19:46 +03:00
fix: Cap diffArrays memo size for large changed ranges (#14760)
* fix: Cap diffArrays memo size for large changed ranges * fix: Count memo boundary row and column in diffArrays cap
This commit is contained in:
1 parent
63de3fcd37
commit
168dc32111
3 files changed
+57
No files matched your search
@@ -305,6 +305,14 @@ describe("ChangesetHelper.getChangeset", () => {
|
||||
expect(ChangesetHelper.getChangeset(after, before)).toBeNull();
|
||||
});
|
||||
|
||||
it("returns null when every paragraph of a large document changes", () => {
|
||||
const many = Array.from({ length: 3000 }, (_, i) => `line ${i}`);
|
||||
const before = paras(...many);
|
||||
const after = paras(...many.map((text) => `${text} rewritten`));
|
||||
|
||||
expect(ChangesetHelper.getChangeset(after, before)).toBeNull();
|
||||
});
|
||||
|
||||
it("still computes a changeset for a small change to a large document", () => {
|
||||
const before = paras(...texts);
|
||||
const after = paras(...texts.slice(0, -1), "a different final paragraph");
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
import { applyPatch, createPatch } from "./index";
|
||||
import { clone } from "./util";
|
||||
|
||||
describe("diffArrays", () => {
|
||||
it("returns the minimal patch for a small array change", () => {
|
||||
const input = ["a", "b", "c", "d"];
|
||||
const output = ["a", "x", "c", "d", "e"];
|
||||
const patch = createPatch(input, output);
|
||||
|
||||
expect(patch).toEqual([
|
||||
{ op: "replace", path: "/1", value: "x" },
|
||||
{ op: "add", path: "/-", value: "e" },
|
||||
]);
|
||||
|
||||
const result = clone(input);
|
||||
applyPatch(result, patch);
|
||||
expect(result).toEqual(output);
|
||||
});
|
||||
|
||||
it("throws quickly instead of building a huge memo", () => {
|
||||
const input = Array.from({ length: 3000 }, (_, i) => `before ${i}`);
|
||||
const output = Array.from({ length: 3000 }, (_, i) => `after ${i}`);
|
||||
const started = Date.now();
|
||||
|
||||
expect(() => createPatch(input, output)).toThrow(/max complexity/);
|
||||
expect(Date.now() - started).toBeLessThan(1000);
|
||||
});
|
||||
|
||||
it("still diffs large arrays when the changed range is small", () => {
|
||||
const input = Array.from({ length: 3000 }, (_, i) => `item ${i}`);
|
||||
const output = [...input];
|
||||
output[1500] = "changed";
|
||||
|
||||
expect(createPatch(input, output)).toEqual([
|
||||
{ op: "replace", path: "/1500", value: "changed" },
|
||||
]);
|
||||
});
|
||||
});
|
||||
@@ -179,6 +179,9 @@ function isArrayRemove(
|
||||
return array_operation.op === "remove";
|
||||
}
|
||||
|
||||
/** Maximum number of memo cells diffArrays may allocate. */
|
||||
const MAX_ARRAY_DIFF_CELLS = 1_000_000;
|
||||
|
||||
interface DynamicAlternative {
|
||||
/** Previous i coordinate (-1 means no previous) */
|
||||
prevI: number;
|
||||
@@ -235,6 +238,7 @@ function buildOperations(
|
||||
* resulting in an array of 'remove' operations.
|
||||
*
|
||||
* @returns a list of add/remove/replace operations.
|
||||
* @throws if the changed ranges are too large to diff in bounded memory.
|
||||
*/
|
||||
export function diffArrays<T>(
|
||||
input: T[],
|
||||
@@ -275,6 +279,13 @@ export function diffArrays<T>(
|
||||
const subInput = input_end - start;
|
||||
const subOutput = output_end - start;
|
||||
|
||||
// The memo below holds one object per cell, so cap it to bound memory.
|
||||
if ((subInput + 1) * (subOutput + 1) > MAX_ARRAY_DIFF_CELLS) {
|
||||
throw new Error(
|
||||
`Array diff exceeds max complexity: ${subInput} x ${subOutput} changed items`
|
||||
);
|
||||
}
|
||||
|
||||
// oxlint-disable-next-line unicorn/no-new-array
|
||||
const memo: Array<Array<DynamicAlternative>> = new Array(subInput + 1);
|
||||
for (let i = 0; i <= subInput; i++) {
|
||||
|
||||
Reference in new issue
Block a user