From db1e4ee5bf98196466f732da9a909535eef54643 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Tue, 25 Oct 2011 23:56:07 -0700 Subject: [PATCH 1/2] More bulletproof `util.inspect()` function. Use the *real* versions of the Date and RegExp functions, from the prototype. This defends against code like: var d = new Date() d.toUTCString = null util.inspect(d) // TypeError: toUTCString is not a function --- lib/util.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/util.js b/lib/util.js index 07e2e3ba9903..7fd432a4af8b 100644 --- a/lib/util.js +++ b/lib/util.js @@ -180,10 +180,10 @@ function formatValue(ctx, value, recurseTimes) { return ctx.stylize('[Function' + name + ']', 'special'); } if (isRegExp(value)) { - return ctx.stylize('' + value, 'regexp'); + return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp'); } if (isDate(value)) { - return ctx.stylize(value.toUTCString(), 'date'); + return ctx.stylize(Date.prototype.toUTCString.call(value), 'date'); } if (isError(value)) { return formatError(value); @@ -206,12 +206,12 @@ function formatValue(ctx, value, recurseTimes) { // Make RegExps say that they are RegExps if (isRegExp(value)) { - base = ' ' + value; + base = ' ' + RegExp.prototype.toString.call(value); } // Make dates with properties first say the date if (isDate(value)) { - base = ' ' + value.toUTCString(); + base = ' ' + Date.prototype.toUTCString.call(value); } // Make error with message first say the error @@ -225,7 +225,7 @@ function formatValue(ctx, value, recurseTimes) { if (recurseTimes < 0) { if (isRegExp(value)) { - return ctx.stylize('' + value, 'regexp'); + return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp'); } else { return ctx.stylize('[Object]', 'special'); } From 5c0a7453c70631e82169bc7ae020bb9b6724899c Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Wed, 26 Oct 2011 12:45:51 -0700 Subject: [PATCH 2/2] Add test cases for #1944. --- test/simple/test-util-inspect.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/simple/test-util-inspect.js b/test/simple/test-util-inspect.js index 8c98ed21c05d..a2912f762c13 100644 --- a/test/simple/test-util-inspect.js +++ b/test/simple/test-util-inspect.js @@ -62,3 +62,16 @@ assert.ok(ex.indexOf('[type]') != -1); // GH-1941 // should not throw: assert.equal(util.inspect(Object.create(Date.prototype)), '{}') + +// GH-1944 +assert.doesNotThrow(function () { + var d = new Date(); + d.toUTCString = null; + util.inspect(d); +}); + +assert.doesNotThrow(function () { + var r = /regexp/; + r.toString = null; + util.inspect(r); +});