diff options
author | Sakthipriyan Vairamani <thechargingvolcano@gmail.com> | 2015-07-09 03:23:48 +0530 |
---|---|---|
committer | Sakthipriyan Vairamani <thechargingvolcano@gmail.com> | 2015-08-04 10:21:37 +0530 |
commit | ea05e760cdb732f58cfe36da1a8ecafe76adc1ac (patch) | |
tree | e7e16e6001977362b1d99729cd1f3eca099f29ac /lib | |
parent | 64cf71195c83a2ff06319cf8719ff31058c9ff80 (diff) | |
download | android-node-v8-ea05e760cdb732f58cfe36da1a8ecafe76adc1ac.tar.gz android-node-v8-ea05e760cdb732f58cfe36da1a8ecafe76adc1ac.tar.bz2 android-node-v8-ea05e760cdb732f58cfe36da1a8ecafe76adc1ac.zip |
repl: don't clobber RegExp.$ properties
In REPL, if we evaluate the `RegExp` object's predefined properties,
and if they happen to have the same expression, for example,
> RegExp.$1
'RegExp.$1'
then doing `eval(RegExp.$1)` would evaluate `RegExp.$1` recursively and
eventually throw `RangeError: Maximum call stack size exceeded`.
This patch stores the old values of `RegExp`'s predefined proprties in
an array and restores them just before the current expression entered
by user is evaluated.
Fixes: https://github.com/nodejs/io.js/issues/597
PR-URL: https://github.com/nodejs/io.js/pull/2137
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/repl.js | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/lib/repl.js b/lib/repl.js index 1d78a00195..366b2b7b0a 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -115,6 +115,12 @@ function REPLServer(prompt, // just for backwards compat, see github.com/joyent/node/pull/7127 self.rli = this; + const savedRegExMatches = ['', '', '', '', '', '', '', '', '', '']; + const sep = '\u0000\u0000\u0000'; + const regExMatcher = new RegExp(`^${sep}(.*)${sep}(.*)${sep}(.*)${sep}(.*)` + + `${sep}(.*)${sep}(.*)${sep}(.*)${sep}(.*)` + + `${sep}(.*)$`); + eval_ = eval_ || defaultEval; function defaultEval(code, context, file, cb) { @@ -148,6 +154,10 @@ function REPLServer(prompt, break; } + // This will set the values from `savedRegExMatches` to corresponding + // predefined RegExp properties `RegExp.$1`, `RegExp.$2` ... `RegExp.$9` + regExMatcher.test(savedRegExMatches.join(sep)); + if (!err) { try { if (self.useGlobal) { @@ -166,6 +176,12 @@ function REPLServer(prompt, } } + // After executing the current expression, store the values of RegExp + // predefined properties back in `savedRegExMatches` + for (let idx = 1; idx < savedRegExMatches.length; idx += 1) { + savedRegExMatches[idx] = RegExp[`$${idx}`]; + } + cb(err, result); } |