Skip to content
This repository was archived by the owner on Apr 16, 2026. It is now read-only.
This repository was archived by the owner on Apr 16, 2026. It is now read-only.

Julia mode - deleting a parenthesis & typing a curly bracket causes stack overflow #6428

Closed

Description

@RomansWorks

Hi, thanks for the great work on CodeMirror and the Julia mode :)

While using Pluto.js (issue JuliaPluto/Pluto.jl#490) I came across a stack overflow that causes the editor to become unresponsive.

Steps to reproduce:

  1. Go to https://codemirror.net/mode/julia/
  2. Delete all contents, type in a(){}(). (thanks to @fonsp for the MWE)
  3. Delete the first paranthesis using backspace, then type {

Expected: typed character ('{') to appear.
Actual: Editor gets stuck, console shows:

codemirror.js:869 Uncaught RangeError: Maximum call stack size exceeded
    at String.match (<anonymous>)
    at StringStream.match (codemirror.js:869)
    at Object.tokenCallOrDef [as tokenize] (julia.js:258)
    at Object.tokenCallOrDef [as tokenize] (julia.js:292)
    at Object.tokenCallOrDef [as tokenize] (julia.js:292)
    at Object.tokenCallOrDef [as tokenize] (julia.js:292)
    at Object.tokenCallOrDef [as tokenize] (julia.js:292)
    at Object.tokenCallOrDef [as tokenize] (julia.js:292)
    at Object.tokenCallOrDef [as tokenize] (julia.js:292)
    at Object.tokenCallOrDef [as tokenize] (julia.js:292)

Environment: MacOS Mojave, Chrome 85.0.4183.121, Safari 14610.1.28.1.9

Now, I don't fully understand how that code works so my solution is probably incorrect (and hence not in a pull request), but here's a shot:
The infinite loop seems to happen in tokenCallOrDef. If none of the conditionals match, the function doesn't return but just calls itself with the same parameters. The following code checks whether no progress on parsing was made via the conditionals and if so returns control to tokenBase instead of calling again:

function tokenCallOrDef(stream, state) {
    var previousPos = state.charsAdvanced;
    var match = stream.match(/^(\(\s*)/);
    if (match) {
      if (state.firstParenPos < 0)
        state.firstParenPos = state.scopes.length;
      state.scopes.push('(');
      state.charsAdvanced += match[1].length;
    }
    if (currentScope(state) == '(' && stream.match(/^\)/)) {
      state.scopes.pop();
      state.charsAdvanced += 1;
      if (state.scopes.length <= state.firstParenPos) {
        var isDefinition = stream.match(/^(\s*where\s+[^\s=]+)*\s*?=(?!=)/, false);
        stream.backUp(state.charsAdvanced);
        state.firstParenPos = -1;
        state.charsAdvanced = 0;
        state.tokenize = tokenBase;
        if (isDefinition)
          return "def";
        return "builtin";
      }
    }
    // Unfortunately javascript does not support multiline strings, so we have
    // to undo anything done upto here if a function call or definition splits
    // over two or more lines.
    if (stream.match(/^$/g, false)) {
      stream.backUp(state.charsAdvanced);
      while (state.scopes.length > state.firstParenPos)
        state.scopes.pop();
      state.firstParenPos = -1;
      state.charsAdvanced = 0;
      state.tokenize = tokenBase;
      return "builtin";
    }
    state.charsAdvanced += stream.match(/^([^()]*)/)[1].length;
    if (state.charsAdvanced == previousPos) {
        // Couldn't make progress using any of the above so abort
        state.tokenize = tokenBase;
        return state.tokenize(stream, state);
    }
    return state.tokenize(stream, state);
  }

Activity

  1. added a commit that references this issue on Oct 31, 2020
    14d7274
  2. added a commit that references this issue on May 1, 2024
    f3dde7c
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions