Skip to content

Regex RangeError for large sourcemaps #93

Closed

Description

@tanem

Hi. We're using webpack's devtool: #cheap-module-inline-source-map option to add an inline base64 encoded source map to the bottom of our main app file. There's a decent amount of code there, so the sourcemap is a pretty gigantic string.

We ended up seeing the following error in the console:

RangeError: Maximum call stack size exceeded
    at RegExp.exec (native)

Related to this regex:

var re = /(?:\/\/[@#][ \t]+sourceMappingURL=([^\s'"]+?)[ \t]*$)|(?:\/\*[@#][ \t]+sourceMappingURL=([^\*]+?)[ \t]*(?:\*\/)[ \t]*$)/mg;

Changing one character in the regex fixes the issue (...[^\s'"]+?... to ...[^\s'"]*?...):

var re = /(?:\/\/[@#][ \t]+sourceMappingURL=([^\s'"]*?)[ \t]*$)|(?:\/\*[@#][ \t]+sourceMappingURL=([^\*]+?)[ \t]*(?:\*\/)[ \t]*$)/mg;

I haven't been able to figure out why that is yet though (e.g. I don't think there's any catastrophic backtracking going on in the original version?) 😕

Just wondering if anyone else can shed some light on this? Cheers. (PS this change also works for the version currently published to npm).

Activity

  1. tanem commented on Aug 20, 2015

    @tanem
    Author

    Whoops, just realised this will probably be fixed by #86. Will check tomorrow.

  2. SkeLLLa commented on May 31, 2017

    @SkeLLLa

    This issue is still present. Are there any plans for fixing this?

    Here's more detailed stacktrace, if it helps:

    "RangeError: Maximum call stack size exceeded"
      "at RegExp.exec (<anonymous>)",
      "at RegExp.[Symbol.match] (native)",
      "at String.match (native)",
      "at urlParse (/myproject/node_modules/source-map-support/node_modules/source-map/lib/util.js:33:20)",
      "at normalize (/myproject/node_modules/source-map-support/node_modules/source-map/lib/util.js:82:13)",
      "at Object.join (/myproject/node_modules/source-map-support/node_modules/source-map/lib/util.js:174:7)",
      "at BasicSourceMapConsumer.SourceMapConsumer_originalPositionFor [as originalPositionFor] (/myproject/node_modules/source-map-support/node_modules/source-map/lib/source-map-consumer.js:621:27)",
      "at mapSourcePosition (/myproject/node_modules/source-map-support/source-map-support.js:195:42)"
    
  3. gibbtek commented on Jan 26, 2018

    @gibbtek

    I can confirm I have the same issue. It happens randomly i.e. not every run.

    The line that causes it for me is when executing the regex:

    /(?:\/\/[@#][ \t]+sourceMappingURL=([^\s'"]+?)[ \t]*$)|(?:\/\*[@#][ \t]+sourceMappingURL=([^\*]+?)[ \t]*(?:\*\/)[ \t]*$)/mg

    • node-source-map-support: 0.4.16
    • Ionic App
    • karma-source-map-support: 1.2.0
    • jasmine: 2.7.0
  4. telb99 commented on Jul 26, 2019

    @telb99

    This is still a problem with Angular 7 unit tests, which uses jasmine, karma, and webpack and creates very large bundles, on a small project, the bundle for vendor.js is over 17MB.

  5. darrengriffinhut42 commented on Jul 29, 2019

    @darrengriffinhut42

    I agree with @telb99. This i still an issue in Angular 8 and needs fixing. @LinusU, please resolve.

  6. LinusU commented on Jul 29, 2019

    @LinusU
    Collaborator

    Happy to receive a PR for this 🙌

  7. added a commit that references this issue on Jul 30, 2019
  8. added a commit that references this issue on Jul 30, 2019
    4852362
  9. LinusU commented on Jul 31, 2019

    @LinusU
    Collaborator

    This should be fixed in version 0.5.13, please report back if the issue persists!

  10. leontastic commented on Jun 7, 2023

    @leontastic

    Just checking in to report that this issue still occurs with the new RegExp in very obscure scenarios. In the case I encountered, it was caused by this line in the emotion/react repo's createEmotionProps function that uses new Error().stack to update component labels while performing SSR builds with ts-node and next.js.

    Interestingly, I was able to intercept the specific file data that caused the error by using local builds of ts-node and a local build of the fork of this repo @cspotcode/source-map-support that ts-node uses, and was not able to reproduce the RangeError with this regex independently with just Node.js.

    However, overriding Error.prepareStackTrace to skip source-map-support's sourcemap transformation logic for stack traces demanded by createEmotionProps reliably resolves the error for me:

    if (Error.prepareStackTrace) {
      const originalPrepareStackTrace = Error.prepareStackTrace;
      Error.prepareStackTrace = function (error, trace) {
        if (error.stack?.includes('createEmotionProps')) {
          return error.stack;
        }
        return originalPrepareStackTrace(error, trace);
      };
    }

    Based on this V8 bug I think RegExp.exec only throws RangeError: Maximum call stack size exceeded when backtracking often over large strings, so I think the current RegExp may still be too greedy for certain large inputs.

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions