From db31de634ea970569d4080817e4279425e7a9103 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Sat, 8 Oct 2022 20:24:02 +0200 Subject: [PATCH] readline: refactor to avoid unsafe regex primordials Refs: https://github.com/nodejs/node/pull/43475 Backport-PR-URL: https://github.com/nodejs/node/pull/44926 PR-URL: https://github.com/nodejs/node/pull/43475 --- lib/readline.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/readline.js b/lib/readline.js index f80f383170814b..e54c2defe14aca 100644 --- a/lib/readline.js +++ b/lib/readline.js @@ -48,12 +48,12 @@ const { NumberIsNaN, ObjectDefineProperty, ObjectSetPrototypeOf, - RegExpPrototypeTest, + RegExpPrototypeExec, + RegExpPrototypeSymbolReplace, + RegExpPrototypeSymbolSplit, StringPrototypeCodePointAt, StringPrototypeEndsWith, - StringPrototypeMatch, StringPrototypeRepeat, - StringPrototypeReplace, StringPrototypeSlice, StringPrototypeSplit, StringPrototypeStartsWith, @@ -642,12 +642,12 @@ Interface.prototype._normalWrite = function(b) { let string = this._decoder.write(b); if (this._sawReturnAt && DateNow() - this._sawReturnAt <= this.crlfDelay) { - string = StringPrototypeReplace(string, /^\n/, ''); + string = RegExpPrototypeSymbolReplace(/^\n/, string, ''); this._sawReturnAt = 0; } // Run test() on the new string chunk, not on the entire line buffer. - const newPartContainsEnding = RegExpPrototypeTest(lineEnding, string); + const newPartContainsEnding = RegExpPrototypeExec(lineEnding, string) !== null; if (this._line_buffer) { string = this._line_buffer + string; @@ -773,7 +773,7 @@ Interface.prototype._wordLeft = function() { const leading = StringPrototypeSlice(this.line, 0, this.cursor); const reversed = ArrayPrototypeJoin( ArrayPrototypeReverse(ArrayFrom(leading)), ''); - const match = StringPrototypeMatch(reversed, /^\s*(?:[^\w\s]+|\w+)?/); + const match = RegExpPrototypeExec(/^\s*(?:[^\w\s]+|\w+)?/, reversed); this._moveCursor(-match[0].length); } }; @@ -782,7 +782,7 @@ Interface.prototype._wordLeft = function() { Interface.prototype._wordRight = function() { if (this.cursor < this.line.length) { const trailing = StringPrototypeSlice(this.line, this.cursor); - const match = StringPrototypeMatch(trailing, /^(?:\s+|[^\w\s]+|\w+)\s*/); + const match = RegExpPrototypeExec(/^(?:\s+|[^\w\s]+|\w+)\s*/, trailing); this._moveCursor(match[0].length); } }; @@ -818,7 +818,7 @@ Interface.prototype._deleteWordLeft = function() { let leading = StringPrototypeSlice(this.line, 0, this.cursor); const reversed = ArrayPrototypeJoin( ArrayPrototypeReverse(ArrayFrom(leading)), ''); - const match = StringPrototypeMatch(reversed, /^\s*(?:[^\w\s]+|\w+)?/); + const match = RegExpPrototypeExec(/^\s*(?:[^\w\s]+|\w+)?/, reversed); leading = StringPrototypeSlice(leading, 0, leading.length - match[0].length); this.line = leading + StringPrototypeSlice(this.line, this.cursor, @@ -832,7 +832,7 @@ Interface.prototype._deleteWordLeft = function() { Interface.prototype._deleteWordRight = function() { if (this.cursor < this.line.length) { const trailing = StringPrototypeSlice(this.line, this.cursor); - const match = StringPrototypeMatch(trailing, /^(?:\s+|\W+|\w+)\s*/); + const match = RegExpPrototypeExec(/^(?:\s+|\W+|\w+)\s*/, trailing); this.line = StringPrototypeSlice(this.line, 0, this.cursor) + StringPrototypeSlice(trailing, match[0].length); this._refreshLine(); @@ -1272,7 +1272,7 @@ Interface.prototype._ttyWrite = function(s, key) { // falls through default: if (typeof s === 'string' && s) { - const lines = StringPrototypeSplit(s, /\r\n|\n|\r/); + const lines = RegExpPrototypeSymbolSplit(/\r\n|\n|\r/, s); for (let i = 0, len = lines.length; i < len; i++) { if (i > 0) { this._line();