sql-formatter-org / sql-formatter

A whitespace formatter for different query languages
https://sql-formatter-org.github.io/sql-formatter/
MIT License
2.32k stars 398 forks source link

Supplying empty regex crashes browser #754

Closed JulianVGit closed 3 months ago

JulianVGit commented 3 months ago

When supplying and empty regex as a custom param for syntax, the browser crashes:

format('SELECT * FROM whatever', {
  language: 'sql',
  paramTypes: {
      custom: [{regex: ''}]
  },
}
nene commented 3 months ago

More correctly, the code will enter an infinite loop, because you have defined a parameter to be an empty string, and there are infinitely many empty strings in any string. So the formatter will correctly detect infinite amount of these custom parameters.

The formatter could possibly check whether the supplied regex is an empty string, and throw a more friendly error message instead of getting stuck in infinite loop. If you're really interested in getting a better error in this specific case, I'm open to a pull request.

However there are infinitely many regexes that would end up matching empty string (for example (|)) and therefore triggering an infinite loop. It's not really feasible to properly validate them all.

nene commented 3 months ago

Oh well. As I'm already looking into this code, might as well add this little extra check.