-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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]>
- Loading branch information
1 parent
5471c4a
commit c78d8b7
Showing
2 changed files
with
46 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const ArrayStream = require('../common/arraystream'); | ||
const assert = require('assert'); | ||
|
||
common.skipIfDumbTerminal(); | ||
|
||
const readline = require('readline'); | ||
const rli = new readline.Interface({ | ||
terminal: true, | ||
input: new ArrayStream(), | ||
}); | ||
|
||
let recursionDepth = 0; | ||
|
||
// Minimal reproduction for #46731 | ||
const testInput = ' \n}\n'; | ||
const numberOfExpectedLines = testInput.match(/\n/g).length; | ||
|
||
rli.on('line', () => { | ||
// Abort in case of infinite loop | ||
if (recursionDepth > numberOfExpectedLines) { | ||
return; | ||
} | ||
recursionDepth++; | ||
// Write something recursively to readline | ||
rli.write('foo'); | ||
}); | ||
|
||
|
||
rli.write(testInput); | ||
|
||
assert.strictEqual(recursionDepth, numberOfExpectedLines); |