lib: refactor to avoid unsafe regex primordials

PR-URL: https://github.com/nodejs/node/pull/43475
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
This commit is contained in:
Antoine du Hamel 2022-06-27 17:16:06 +02:00 committed by GitHub
parent 411fb21723
commit a055337a02
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
39 changed files with 206 additions and 208 deletions

View file

@ -21,12 +21,12 @@ const {
NumberIsFinite,
NumberIsNaN,
ObjectSetPrototypeOf,
RegExpPrototypeTest,
RegExpPrototypeExec,
RegExpPrototypeSymbolReplace,
RegExpPrototypeSymbolSplit,
StringPrototypeCodePointAt,
StringPrototypeEndsWith,
StringPrototypeMatch,
StringPrototypeRepeat,
StringPrototypeReplace,
StringPrototypeSlice,
StringPrototypeSplit,
StringPrototypeStartsWith,
@ -590,12 +590,12 @@ class Interface extends InterfaceConstructor {
this[kSawReturnAt] &&
DateNow() - this[kSawReturnAt] <= this.crlfDelay
) {
string = StringPrototypeReplace(string, /^\n/, '');
string = RegExpPrototypeSymbolReplace(/^\n/, string, '');
this[kSawReturnAt] = 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[kLine_buffer]) {
string = this[kLine_buffer] + string;
@ -738,7 +738,7 @@ class Interface extends InterfaceConstructor {
ArrayPrototypeReverse(ArrayFrom(leading)),
''
);
const match = StringPrototypeMatch(reversed, /^\s*(?:[^\w\s]+|\w+)?/);
const match = RegExpPrototypeExec(/^\s*(?:[^\w\s]+|\w+)?/, reversed);
this[kMoveCursor](-match[0].length);
}
}
@ -746,7 +746,7 @@ class Interface extends InterfaceConstructor {
[kWordRight]() {
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[kMoveCursor](match[0].length);
}
}
@ -791,7 +791,7 @@ class Interface extends InterfaceConstructor {
ArrayPrototypeReverse(ArrayFrom(leading)),
''
);
const match = StringPrototypeMatch(reversed, /^\s*(?:[^\w\s]+|\w+)?/);
const match = RegExpPrototypeExec(/^\s*(?:[^\w\s]+|\w+)?/, reversed);
leading = StringPrototypeSlice(
leading,
0,
@ -809,7 +809,7 @@ class Interface extends InterfaceConstructor {
if (this.cursor < this.line.length) {
this[kBeforeEdit](this.line, this.cursor);
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);
@ -1322,7 +1322,7 @@ class Interface extends InterfaceConstructor {
// 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[kLine]();