Skip to content

Commit

Permalink
Parse comments without recursion to avoid RangeError.
Browse files Browse the repository at this point in the history
Fixes #993
  • Loading branch information
kzc committed Apr 16, 2016
1 parent 4b4528e commit e4fa4b1
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 29 deletions.
67 changes: 38 additions & 29 deletions lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ function tokenizer($TEXT, filename, html5_comments, shebang) {
S.col = S.tokcol + (S.pos - S.tokpos);
S.comments_before.push(token(type, ret, true));
S.regex_allowed = regex_allowed;
return next_token();
return next_token;
};

var skip_multiline_comment = with_eof_error("Unterminated multiline comment", function(){
Expand All @@ -439,7 +439,7 @@ function tokenizer($TEXT, filename, html5_comments, shebang) {
S.comments_before.push(token("comment2", text, true));
S.regex_allowed = regex_allowed;
S.newline_before = nlb;
return next_token();
return next_token;
});

function read_name() {
Expand Down Expand Up @@ -548,36 +548,45 @@ function tokenizer($TEXT, filename, html5_comments, shebang) {
function next_token(force_regexp) {
if (force_regexp != null)
return read_regexp(force_regexp);
skip_whitespace();
start_token();
if (html5_comments) {
if (looking_at("<!--")) {
forward(4);
return skip_line_comment("comment3");
for (;;) {
skip_whitespace();
start_token();
if (html5_comments) {
if (looking_at("<!--")) {
forward(4);
skip_line_comment("comment3");
continue;
}
if (looking_at("-->") && S.newline_before) {
forward(3);
skip_line_comment("comment4");
continue;
}
}
if (looking_at("-->") && S.newline_before) {
forward(3);
return skip_line_comment("comment4");
var ch = peek();
if (!ch) return token("eof");
var code = ch.charCodeAt(0);
switch (code) {
case 34: case 39: return read_string(ch);
case 46: return handle_dot();
case 47: {
var tok = handle_slash();
if (tok === next_token) continue;
return tok;
}
}
}
var ch = peek();
if (!ch) return token("eof");
var code = ch.charCodeAt(0);
switch (code) {
case 34: case 39: return read_string(ch);
case 46: return handle_dot();
case 47: return handle_slash();
}
if (is_digit(code)) return read_num();
if (PUNC_CHARS(ch)) return token("punc", next());
if (OPERATOR_CHARS(ch)) return read_operator();
if (code == 92 || is_identifier_start(code)) return read_word();

if (shebang) {
if (S.pos == 0 && looking_at("#!")) {
forward(2);
return skip_line_comment("comment5");
if (is_digit(code)) return read_num();
if (PUNC_CHARS(ch)) return token("punc", next());
if (OPERATOR_CHARS(ch)) return read_operator();
if (code == 92 || is_identifier_start(code)) return read_word();
if (shebang) {
if (S.pos == 0 && looking_at("#!")) {
forward(2);
skip_line_comment("comment5");
continue;
}
}
break;
}
parse_error("Unexpected character '" + ch + "'");
};
Expand Down
19 changes: 19 additions & 0 deletions test/mocha/huge-number-of-comments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
var Uglify = require('../../');
var assert = require("assert");

describe("Huge number of comments.", function() {
it("Should parse and compress code with thousands of consecutive comments", function() {
var js = 'function lots_of_comments(x) { return 7 -';
var i;
for (i = 1; i <= 5000; ++i) { js += "// " + i + "\n"; }
for (; i <= 10000; ++i) { js += "/* " + i + " */ /**/"; }
js += "x; }";
var result = Uglify.minify(js, {
fromString: true,
mangle: false,
compress: {}
});
assert.strictEqual(result.code, "function lots_of_comments(x){return 7-x}");
});
});

0 comments on commit e4fa4b1

Please sign in to comment.