From dd0b95498ec85d86f15f49aa1e1bb840149c8208 Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Sat, 10 Jun 2017 00:49:22 -0400 Subject: [PATCH] Fixes excessive memory usages with diff regex --- src/git/parsers/diffParser.ts | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/git/parsers/diffParser.ts b/src/git/parsers/diffParser.ts index 855174f..9266822 100644 --- a/src/git/parsers/diffParser.ts +++ b/src/git/parsers/diffParser.ts @@ -12,15 +12,22 @@ export class GitDiffParser { const chunks: GitDiffChunk[] = []; let match: RegExpExecArray | null = null; + + let chunk: string; + let currentStart: number; + let previousStart: number; + do { match = unifiedDiffRegex.exec(`${data}\n@@`); if (match == null) break; - const previousStart = +match[1]; - const currentStart = +match[3]; + // Stops excessive memory usage + // https://bugs.chromium.org/p/v8/issues/detail?id=2869 + chunk = (' ' + match[5]).substr(1); + currentStart = parseInt(match[3], 10); + previousStart = parseInt(match[1], 10); - const chunk = match[5]; - chunks.push(new GitDiffChunk(chunk, { start: currentStart, end: currentStart + +match[4] }, { start: previousStart, end: previousStart + +match[2] })); + chunks.push(new GitDiffChunk(chunk, { start: currentStart, end: currentStart + parseInt(match[4], 10) }, { start: previousStart, end: previousStart + parseInt(match[2], 10) })); } while (match != null); if (!chunks.length) return undefined;