Skip to content

repl: repl clobbers RegExp.$1 #597

Closed

Description

@bnoordhuis
$ out/Release/iojs
> RegExp.$1
'RegExp.$1'
> eval(RegExp.$1)
# snip
    at <error: RangeError: Maximum call stack size exceeded>

Activity

  1. added
    replIssues and PRs related to the REPL subsystem.
    confirmed-bugIssues and PRs for confirmed bugs.
    on Jan 24, 2015
  2. vkurchatkin commented on Jan 24, 2015

    @vkurchatkin
    Contributor

    is this the same issue: nodejs/node-v0.x-archive#4088 ?

  3. bnoordhuis commented on Jan 24, 2015

    @bnoordhuis
    MemberAuthor

    Yes, it is. I'd forgotten about that other issue, actually.

  4. vkurchatkin commented on Feb 21, 2015

    @vkurchatkin
    Contributor

    As an idea: we can execute regexps in a different context

  5. chrisdickinson commented on Jul 8, 2015

    @chrisdickinson
    Contributor

    Given that we have a patch, this would make a good first bug for anyone with the time to PR it in.

  6. thefourtheye commented on Jul 8, 2015

    @thefourtheye
    Contributor

    I was looking at it the other day, I was able to reproduce the problem but couldn't understand the fix suggested. I ll spend sometime this week to understand it.

  7. bnoordhuis commented on Jul 8, 2015

    @bnoordhuis
    MemberAuthor

    Reproducing here for posterity:

    diff --git a/lib/repl.js b/lib/repl.js
    index 361745f..fe3077c 100644
    --- a/lib/repl.js
    +++ b/lib/repl.js
    @@ -104,9 +104,14 @@ function REPLServer(prompt, stream, eval_, useGlobal, ignoreUndefined) {
    
       self.useGlobal = !!useGlobal;
       self.ignoreUndefined = !!ignoreUndefined;
    +  self._saved = [];
    
       self.eval = eval_ || function(code, context, file, cb) {
         var err, result;
    +
    +    // restore RegExp.$1 to RegExp.$9
    +    /^\u0000(.*)\u0000(.*)\u0000(.*)\u0000(.*)\u0000(.*)\u0000(.*)\u0000(.*)\u0000(.*)\u0000(.*)$/.test(self._saved.join('\u0000'));
    +
         try {
           if (self.useGlobal) {
             result = vm.runInThisContext(code, file);
    @@ -116,6 +121,11 @@ function REPLServer(prompt, stream, eval_, useGlobal, ignoreUndefined) {
         } catch (e) {
           err = e;
         }
    +
    +    for (var i = 1; i <= 9; ++i) {
    +      self._saved[i] = RegExp['$' + i];
    +    }
    +
         cb(err, result);
       };

    @thefourtheye It works by creating a string from the 9 saved values, then doing a capturing match against that, thereby restoring the values of RegExp.$1 to RegExp.$9.

    Aside: using '\u0000' as the separator is probably not unique enough in practice.

  8. thefourtheye commented on Jul 8, 2015

    @thefourtheye
    Contributor

    @bnoordhuis Thanks for helping me understand :-)

  9. thefourtheye commented on Aug 4, 2015

    @thefourtheye
    Contributor

    In light of #2137's landing, closing this bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-bugIssues and PRs for confirmed bugs.good first issueIssues that are suitable for first-time contributors.replIssues and PRs related to the REPL subsystem.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions