Skip to content

Commit 333aff0

Browse files
authored
repl: fix .load infinite loop caused by shared use of lineEnding RegExp
Since the lineEnding Regular Expression is declared on the module scope, recursive invocations of its `[kTtyWrite]` method share one instance of this Regular Expression. Since the state of a RegExp is managed by instance, alternately calling RegExpPrototypeExec with the same RegExp on different strings can lead to the state changing unexpectedly. This is the root cause of this infinite loop bug when calling .load on javascript files of certain shapes. PR-URL: #46742 Fixes: #46731 Reviewed-By: Kohei Ueno <kohei.ueno119@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 3860087 commit 333aff0

File tree

2 files changed

+46
-9
lines changed

2 files changed

+46
-9
lines changed

lib/internal/readline/interface.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,18 +1322,22 @@ class Interface extends InterfaceConstructor {
13221322
// falls through
13231323
default:
13241324
if (typeof s === 'string' && s) {
1325+
// Erase state of previous searches.
1326+
lineEnding.lastIndex = 0;
13251327
let nextMatch = RegExpPrototypeExec(lineEnding, s);
1326-
if (nextMatch !== null) {
1327-
this[kInsertString](StringPrototypeSlice(s, 0, nextMatch.index));
1328-
let { lastIndex } = lineEnding;
1329-
while ((nextMatch = RegExpPrototypeExec(lineEnding, s)) !== null) {
1330-
this[kLine]();
1328+
// If no line endings are found, just insert the string as is.
1329+
if (nextMatch === null) {
1330+
this[kInsertString](s);
1331+
} else {
1332+
// Keep track of the end of the last match.
1333+
let lastIndex = 0;
1334+
do {
13311335
this[kInsertString](StringPrototypeSlice(s, lastIndex, nextMatch.index));
13321336
({ lastIndex } = lineEnding);
1333-
}
1334-
if (lastIndex === s.length) this[kLine]();
1335-
} else {
1336-
this[kInsertString](s);
1337+
this[kLine]();
1338+
// Restore lastIndex as the call to kLine could have mutated it.
1339+
lineEnding.lastIndex = lastIndex;
1340+
} while ((nextMatch = RegExpPrototypeExec(lineEnding, s)) !== null);
13371341
}
13381342
}
13391343
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict';
2+
const common = require('../common');
3+
const ArrayStream = require('../common/arraystream');
4+
const assert = require('assert');
5+
6+
common.skipIfDumbTerminal();
7+
8+
const readline = require('readline');
9+
const rli = new readline.Interface({
10+
terminal: true,
11+
input: new ArrayStream(),
12+
});
13+
14+
let recursionDepth = 0;
15+
16+
// Minimal reproduction for #46731
17+
const testInput = ' \n}\n';
18+
const numberOfExpectedLines = testInput.match(/\n/g).length;
19+
20+
rli.on('line', () => {
21+
// Abort in case of infinite loop
22+
if (recursionDepth > numberOfExpectedLines) {
23+
return;
24+
}
25+
recursionDepth++;
26+
// Write something recursively to readline
27+
rli.write('foo');
28+
});
29+
30+
31+
rli.write(testInput);
32+
33+
assert.strictEqual(recursionDepth, numberOfExpectedLines);

0 commit comments

Comments
 (0)