-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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.
- Loading branch information
1 parent
1b87cb6
commit 578c040
Showing
3 changed files
with
66 additions
and
10 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
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,47 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const ArrayStream = require('../common/arraystream'); | ||
const assert = require('assert'); | ||
const join = require('path').join; | ||
const fs = require('fs'); | ||
|
||
common.skipIfDumbTerminal(); | ||
|
||
const tmpdir = require('../common/tmpdir'); | ||
tmpdir.refresh(); | ||
|
||
const terminalCode = '(\u001b[1G\u001b[0J \u001b[1G)'; | ||
const terminalCodeRegex = new RegExp(terminalCode.replace(/\[/g, '\\['), 'g'); | ||
|
||
const repl = require('repl'); | ||
|
||
const inputStream = new ArrayStream(); | ||
const outputStream = new ArrayStream(); | ||
|
||
const r = repl.start({ | ||
prompt: '', | ||
input: inputStream, | ||
output: outputStream, | ||
terminal: true, | ||
useColors: false | ||
}); | ||
|
||
const testFile = 'function a(b) {\n return b }\na(1)\n'; | ||
const testFileName = join(tmpdir.path, 'foo.js'); | ||
fs.writeFileSync(testFileName, testFile); | ||
|
||
const command = `.load ${testFileName}\n`; | ||
let accum = ''; | ||
outputStream.write = (data) => accum += data.replace('\r', ''); | ||
|
||
|
||
r.write(command); | ||
|
||
const expected = command + | ||
'function a(b) {\n' + | ||
' return b }\n' + | ||
'a(1)\n' + | ||
'\n' + | ||
'1\n'; | ||
assert.strictEqual(accum.replace(terminalCodeRegex, ''), expected); | ||
r.close(); |