eriwen/javascript-stacktrace — Issue #28: printStackTrace potentially mutates every argument list on the stack
Author: kevingadd
Created: 2011-10-21 15:32:05+00:00
State: open

The stringifyArguments function mutates arrays in-place, which means it may end up mutating every argument list on the stack when you call printStackTrace. Because it recurses, any arrays or objects passed as arguments will also get mutated. Every call, for example, adds a pair of quotes around every string in the argument list.

Naturally, this breaks EVERYTHING.

Here's a fixed version of stringifyArguments:

stringifyArguments: function(args) {
var result = [];
var slice = Array.prototype.slice;
for (var i = 0; i < args.length; ++i) {
var arg = args[i];
if (arg === undefined) {
result[i] = 'undefined';
} else if (arg === null) {
result[i] = 'null';
} else if (arg.constructor) {
if (arg.constructor === Array) {
if (arg.length < 3) {
result[i] = '[' + this.stringifyArguments(arg) + ']';
} else {
result[i] = '[' + this.stringifyArguments(slice.call(arg, 0, 1)) + '...' + this.stringifyArguments(slice.call(arg, -1)) + ']';
}
} else if (arg.constructor === Object) {
result[i] = '#object';
} else if (arg.constructor === Function) {
result[i] = '#function';
} else if (arg.constructor === String) {
result[i] = '"' + arg + '"';
}
}
}
return result.join(',');
},