Skip to content
This repository was archived by the owner on Oct 20, 2023. It is now read-only.

Commit e274685

Browse files
committed
More effectively guard against infinite recursion in Type.toString
Issue #523
1 parent 7e5f750 commit e274685

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

lib/infer.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,8 @@
498498
Obj.prototype = extend(Type.prototype, {
499499
constructor: Obj,
500500
toString: function(maxDepth) {
501-
if (!maxDepth && this.name) return this.name;
501+
if (maxDepth == null) maxDepth = 0;
502+
if (maxDepth <= 0 && this.name) return this.name;
502503
var props = [], etc = false;
503504
for (var prop in this.props) if (prop != "<i>") {
504505
if (props.length > 5) { etc = true; break; }
@@ -626,17 +627,17 @@
626627
Fn.prototype = extend(Obj.prototype, {
627628
constructor: Fn,
628629
toString: function(maxDepth) {
629-
if (maxDepth) maxDepth--;
630+
if (maxDepth == null) maxDepth = 0;
630631
var str = "fn(";
631632
for (var i = 0; i < this.args.length; ++i) {
632633
if (i) str += ", ";
633634
var name = this.argNames[i];
634635
if (name && name != "?") str += name + ": ";
635-
str += toString(this.args[i], maxDepth, this);
636+
str += maxDepth > -3 ? toString(this.args[i], maxDepth - 1, this) : "?";
636637
}
637638
str += ")";
638639
if (!this.retval.isEmpty())
639-
str += " -> " + toString(this.retval, maxDepth, this);
640+
str += " -> " + (maxDepth > -3 ? toString(this.retval, maxDepth - 1, this) : "?");
640641
return str;
641642
},
642643
getProp: function(prop) {
@@ -674,7 +675,8 @@
674675
Arr.prototype = extend(Obj.prototype, {
675676
constructor: Arr,
676677
toString: function(maxDepth) {
677-
return "[" + toString(this.getProp("<i>"), maxDepth, this) + "]";
678+
if (maxDepth == null) maxDepth = 0;
679+
return "[" + (maxDepth > -3 ? toString(this.getProp("<i>"), maxDepth - 1, this) : "?") + "]";
678680
}
679681
});
680682

0 commit comments

Comments
 (0)