Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix infinite recursion in function inlining #2445

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions lib/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,24 @@ var AST_Token = DEFNODE("Token", "type value line col pos endline endcol endpos
}, null);

var AST_Node = DEFNODE("Node", "start end", {
_clone: function(deep) {
_clone: function(deep, depth) {
if (typeof depth == "undefined") depth = 0;
if (depth++ > 500) {
throw new Error("maximum clone depth exceeded (" +
this.start.file + ":" + this.start.line + ":" + this.start.col + ")");
}
if (deep) {
var self = this.clone();
var self = this.clone(false, depth);
return self.transform(new TreeTransformer(function(node) {
if (node !== self) {
return node.clone(true);
return node.clone(true, depth);
}
}));
}
return new this.CTOR(this);
},
clone: function(deep) {
return this._clone(deep);
clone: function(deep, depth) {
return this._clone(deep, depth);
},
$documentation: "Base class of all AST nodes",
$propdoc: {
Expand Down Expand Up @@ -202,8 +207,8 @@ var AST_LabeledStatement = DEFNODE("LabeledStatement", "label", {
this.body._walk(visitor);
});
},
clone: function(deep) {
var node = this._clone(deep);
clone: function(deep, depth) {
var node = this._clone(deep, depth);
if (deep) {
var label = node.label;
var def = this.label;
Expand Down
6 changes: 5 additions & 1 deletion lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -4243,7 +4243,11 @@ merge(Compressor.prototype, {
}
if (fixed && d.single_use) {
var value = fixed.optimize(compressor);
return value === fixed ? fixed.clone(true) : value;
try {
return value === fixed ? fixed.clone(true) : value;
} catch (x) {
// infinite clone - do nothing
}
}
if (fixed && d.should_replace === undefined) {
var init;
Expand Down
182 changes: 182 additions & 0 deletions test/compress/reduce_vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -3654,3 +3654,185 @@ issue_2440_with_2: {
}
}
}

recursive_inlining_1: {
options = {
reduce_vars: true,
toplevel: true,
unused: true,
}
input: {
!function(){
function foo() { bar(); }
function bar() { foo(); }
console.log("PASS");
}();
}
expect: {
!function() {
console.log("PASS");
}();
}
expect_stdout: "PASS"
}

recursive_inlining_2: {
options = {
reduce_vars: true,
toplevel: true,
unused: true,
}
input: {
!function(){
function foo() { qux(); }
function bar() { foo(); }
function qux() { bar(); }
console.log("PASS");
}();
}
expect: {
!function() {
console.log("PASS");
}();
}
expect_stdout: "PASS"
}

recursive_inlining_3: {
options = {
reduce_vars: true,
toplevel: true,
unused: true,
}
input: {
!function() {
function foo(x) { console.log("foo", x); if (x) bar(x-1); }
function bar(x) { console.log("bar", x); if (x) qux(x-1); }
function qux(x) { console.log("qux", x); if (x) foo(x-1); }
qux(4);
}();
}
expect: {
!function() {
function qux(x) {
console.log("qux", x);
if (x) (function(x) {
console.log("foo", x);
if (x) (function(x) {
console.log("bar", x);
if (x) qux(x - 1);
})(x - 1);
})(x - 1);
}
qux(4);
}();
}
expect_stdout: [
"qux 4",
"foo 3",
"bar 2",
"qux 1",
"foo 0",
]
}

recursive_inlining_4: {
options = {
reduce_vars: true,
toplevel: true,
unused: true,
}
input: {
!function() {
function foo(x) { console.log("foo", x); if (x) bar(x-1); }
function bar(x) { console.log("bar", x); if (x) qux(x-1); }
function qux(x) { console.log("qux", x); if (x) foo(x-1); }
qux(4);
bar(5);
}();
}
expect: {
!function() {
function bar(x) {
console.log("bar", x);
if (x) qux(x - 1);
}
function qux(x) {
console.log("qux", x);
if (x) (function(x) {
console.log("foo", x);
if (x) bar(x - 1);
})(x - 1);
}
qux(4);
bar(5);
}();
}
expect_stdout: [
"qux 4",
"foo 3",
"bar 2",
"qux 1",
"foo 0",
"bar 5",
"qux 4",
"foo 3",
"bar 2",
"qux 1",
"foo 0",
]
}

recursive_inlining_5: {
options = {
reduce_vars: true,
toplevel: true,
unused: true,
}
input: {
!function() {
function foo(x) { console.log("foo", x); if (x) bar(x-1); }
function bar(x) { console.log("bar", x); if (x) qux(x-1); }
function qux(x) { console.log("qux", x); if (x) foo(x-1); }
qux(4);
bar(5);
foo(3);
}();
}
expect: {
!function() {
function foo(x) {
console.log("foo", x);
if (x) bar(x - 1);
}
function bar(x) {
console.log("bar", x);
if (x) qux(x - 1);
}
function qux(x) {
console.log("qux", x);
if (x) foo(x - 1);
}
qux(4);
bar(5);
foo(3);
}();
}
expect_stdout: [
"qux 4",
"foo 3",
"bar 2",
"qux 1",
"foo 0",
"bar 5",
"qux 4",
"foo 3",
"bar 2",
"qux 1",
"foo 0",
"foo 3",
"bar 2",
"qux 1",
"foo 0",
]
}