Skip to content

Commit 09739a2

Browse files
Theo-Steinerdanielleadams
authored andcommitted
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 <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
1 parent 4d0faf4 commit 09739a2

File tree

2 files changed

+46
-9
lines changed

2 files changed

+46
-9
lines changed

lib/internal/readline/interface.js

+13-9
Original file line numberDiff line numberDiff line change
@@ -1329,18 +1329,22 @@ class Interface extends InterfaceConstructor {
13291329
// falls through
13301330
default:
13311331
if (typeof s === 'string' && s) {
1332+
// Erase state of previous searches.
1333+
lineEnding.lastIndex = 0;
13321334
let nextMatch = RegExpPrototypeExec(lineEnding, s);
1333-
if (nextMatch !== null) {
1334-
this[kInsertString](StringPrototypeSlice(s, 0, nextMatch.index));
1335-
let { lastIndex } = lineEnding;
1336-
while ((nextMatch = RegExpPrototypeExec(lineEnding, s)) !== null) {
1337-
this[kLine]();
1335+
// If no line endings are found, just insert the string as is.
1336+
if (nextMatch === null) {
1337+
this[kInsertString](s);
1338+
} else {
1339+
// Keep track of the end of the last match.
1340+
let lastIndex = 0;
1341+
do {
13381342
this[kInsertString](StringPrototypeSlice(s, lastIndex, nextMatch.index));
13391343
({ lastIndex } = lineEnding);
1340-
}
1341-
if (lastIndex === s.length) this[kLine]();
1342-
} else {
1343-
this[kInsertString](s);
1344+
this[kLine]();
1345+
// Restore lastIndex as the call to kLine could have mutated it.
1346+
lineEnding.lastIndex = lastIndex;
1347+
} while ((nextMatch = RegExpPrototypeExec(lineEnding, s)) !== null);
13441348
}
13451349
}
13461350
}
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)