From d3ee6d94986b2832a58ffccce9039cbc69551138 Mon Sep 17 00:00:00 2001 From: tophf Date: Tue, 28 Jan 2020 05:39:36 +0300 Subject: [PATCH] fix an infinite loop in editor search with /^/ or /$/ (#832) --- edit/global-search.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/edit/global-search.js b/edit/global-search.js index d6f18d13..349b8ac1 100644 --- a/edit/global-search.js +++ b/edit/global-search.js @@ -823,11 +823,13 @@ onDOMready().then(() => { if (numApplies === undefined && state.searchInApplies && state.numApplies < 0) { numApplies = 0; const elements = state.find ? document.getElementsByClassName(APPLIES_VALUE_CLASS) : []; + const {rx} = state; for (const el of elements) { const value = el.value; - if (state.rx) { - state.rx.lastIndex = 0; - while (state.rx.exec(value)) numApplies++; + if (rx) { + rx.lastIndex = 0; + // preventing an infinite loop if matched an empty string and didn't advance + for (let m; (m = rx.exec(value)) && ++numApplies && rx.lastIndex > m.index;) { /* NOP */ } } else { let i = -1; while ((i = value.indexOf(state.find, i + 1)) >= 0) numApplies++;