Browse files

fixes #1772: statically catch invalid regular expressions of any sort

  • Loading branch information...
1 parent 4a937ec commit 977db5cc7492ebcaa193f2f7618186ace6e63ea9 @michaelficarra michaelficarra committed Oct 17, 2011
Showing with 24 additions and 15 deletions.
  1. +13 −9 lib/coffee-script/lexer.js
  2. +6 −6 src/lexer.coffee
  3. +5 −0 test/regexps.coffee
View
22 lib/coffee-script/lexer.js
@@ -194,23 +194,27 @@
}
if (!(match = REGEX.exec(this.chunk))) return 0;
_ref4 = match, match = _ref4[0], regex = _ref4[1], flags = _ref4[2];
- if (regex.slice(0, 2) === '/*') {
- this.error('regular expressions cannot begin with `*`');
+ regex = regex.slice(1, -1) || '(?:)';
+ try {
+ new RegExp(regex, flags);
+ } catch (e) {
+ this.error(e.message);
}
- if (regex === '//') regex = '/(?:)/';
- this.token('REGEX', "" + regex + flags);
+ this.token('REGEX', "/" + regex + "/" + flags);
return match.length;
};
Lexer.prototype.heregexToken = function(match) {
- var body, flags, heregex, re, tag, tokens, value, _i, _len, _ref3, _ref4, _ref5, _ref6;
+ var body, flags, heregex, regex, tag, tokens, value, _i, _len, _ref3, _ref4, _ref5, _ref6;
heregex = match[0], body = match[1], flags = match[2];
if (0 > body.indexOf('#{')) {
- re = body.replace(HEREGEX_OMIT, '').replace(/\//g, '\\/');
- if (re.match(/^\*/)) {
- this.error('regular expressions cannot begin with `*`');
+ regex = body.replace(HEREGEX_OMIT, '').replace(/\//g, '\\/') || '(?:)';
+ try {
+ new RegExp(regex, flags);
+ } catch (e) {
+ this.error(e.message);
}
- this.token('REGEX', "/" + (re || '(?:)') + "/" + flags);
+ this.token('REGEX', "/" + regex + "/" + flags);
return heregex.length;
}
this.token('IDENTIFIER', 'RegExp');
View
12 src/lexer.coffee
@@ -199,18 +199,18 @@ exports.Lexer = class Lexer
return 0 if prev and (prev[0] in (if prev.spaced then NOT_REGEX else NOT_SPACED_REGEX))
return 0 unless match = REGEX.exec @chunk
[match, regex, flags] = match
- if regex[..1] is '/*' then @error 'regular expressions cannot begin with `*`'
- if regex is '//' then regex = '/(?:)/'
- @token 'REGEX', "#{regex}#{flags}"
+ regex = regex[1...-1] or '(?:)'
+ try new RegExp regex, flags catch e then @error e.message
+ @token 'REGEX', "/#{regex}/#{flags}"
match.length
# Matches multiline extended regular expressions.
heregexToken: (match) ->
[heregex, body, flags] = match
if 0 > body.indexOf '#{'
- re = body.replace(HEREGEX_OMIT, '').replace(/\//g, '\\/')
- if re.match /^\*/ then @error 'regular expressions cannot begin with `*`'
- @token 'REGEX', "/#{ re or '(?:)' }/#{flags}"
+ regex = body.replace(HEREGEX_OMIT, '').replace(/\//g, '\\/') or '(?:)'
+ try new RegExp regex, flags catch e then @error e.message
+ @token 'REGEX', "/#{regex}/#{flags}"
return heregex.length
@token 'IDENTIFIER', 'RegExp'
@tokens.push ['CALL_START', '(']
View
5 test/regexps.coffee
@@ -41,6 +41,11 @@ test "#584: slashes are allowed unescaped in character classes", ->
test "#1724: regular expressions beginning with `*`", ->
throws -> CoffeeScript.compile '/*/'
+test "#1772: statically catch invalid regular expressions of any sort", ->
+ throws -> CoffeeScript.compile '/?/'
+ throws -> CoffeeScript.compile '/[/'
+ throws -> CoffeeScript.compile '/// ? ///'
+
# Heregexe(n|s)

0 comments on commit 977db5c

Please sign in to comment.