From 2644e242447ac355a415abec7af5cfb8d9849e75 Mon Sep 17 00:00:00 2001 From: Varun Gandhi Date: Thu, 25 Jul 2024 11:50:03 +0800 Subject: [PATCH] fix: Fix Chrome stack overflow during highlighting (#64072) Using the spread operator with large arrays can trigger a stack overflow in Chrome/V8. For example, see: - https://github.com/nodejs/node/issues/16870 In a highlighting context, we can have 10k-100k occurrences in a file, so let's avoid using the spread operator. Fixes https://linear.app/sourcegraph/issue/GRAPH-772 ## Test plan Manually tested against sample file. ![](https://github.com/user-attachments/assets/e096c664-063e-44ed-a991-72629af36651) ## Changelog - Fixes a Chrome-specific stack overflow when highlighting large files. --- .../web/src/repo/blob/codemirror/codeintel/occurrences.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/client/web/src/repo/blob/codemirror/codeintel/occurrences.ts b/client/web/src/repo/blob/codemirror/codeintel/occurrences.ts index b1e1a3eca73..9e253264fcf 100644 --- a/client/web/src/repo/blob/codemirror/codeintel/occurrences.ts +++ b/client/web/src/repo/blob/codemirror/codeintel/occurrences.ts @@ -52,7 +52,13 @@ export class OccurrenceIndex extends Array { previousEndline = current.range.end.line } - super(...nonOverlappingOccurrences(occurrences)) + // CAUTION: Do not "optimize" this to super(...nonOverlappingOccurrences(occurrences)) + // as Chrome will push all elements to a stack, and potentially trigger a stack overflow. + // Similar bug in Nodejs: https://github.com/nodejs/node/issues/16870 + super() + for (const occ of nonOverlappingOccurrences(occurrences)) { + this.push(occ) + } this.lineIndex = lineIndex }