Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
util: make util.inspect() work when "hasOwnProperty" is overwritten
Browse files Browse the repository at this point in the history
  • Loading branch information
TooTallNate committed Sep 8, 2012
1 parent 9a3521c commit fb383a0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/util.js
Expand Up @@ -318,7 +318,7 @@ function formatError(value) {
function formatArray(ctx, value, recurseTimes, visibleKeys, keys) {
var output = [];
for (var i = 0, l = value.length; i < l; ++i) {
if (Object.prototype.hasOwnProperty.call(value, String(i))) {
if (hasOwnProperty(value, String(i))) {
output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
String(i), true));
} else {
Expand Down Expand Up @@ -349,7 +349,7 @@ function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) {
str = ctx.stylize('[Setter]', 'special');
}
}
if (!visibleKeys.hasOwnProperty(key)) {
if (!hasOwnProperty(visibleKeys, key)) {
name = '[' + key + ']';
}
if (!str) {
Expand Down Expand Up @@ -556,3 +556,7 @@ exports._extend = function(origin, add) {
}
return origin;
};

function hasOwnProperty(obj, prop) {
return Object.prototype.hasOwnProperty.call(obj, prop);
}
7 changes: 7 additions & 0 deletions test/simple/test-util-inspect.js
Expand Up @@ -107,3 +107,10 @@ assert.doesNotThrow(function() {
// GH-2225
var x = { inspect: util.inspect };
assert.ok(util.inspect(x).indexOf('inspect') != -1);

// an object with "hasOwnProperty" overwritten should not throw
assert.doesNotThrow(function() {
util.inspect({
hasOwnProperty: null
});
});

0 comments on commit fb383a0

Please sign in to comment.