Skip to content

Commit be30911

Browse files
BridgeARaddaleax
authored andcommitted
util: fix .format() not always calling toString when it should be
This makes sure that `util.format('%s', object)` will always call a user defined `toString` function. It was formerly not the case when the object had the function declared on the super class. At the same time this also makes sure that getters won't be triggered accessing the `constructor` property. PR-URL: #30343 Fixes: #30333 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Denys Otrishko <[email protected]> Reviewed-By: Jeremiah Senkpiel <[email protected]>
1 parent f0181d9 commit be30911

File tree

2 files changed

+95
-22
lines changed

2 files changed

+95
-22
lines changed

lib/internal/util/inspect.js

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1572,6 +1572,31 @@ function reduceToSingleString(
15721572
return `${braces[0]}${ln}${join(output, `,\n${indentation} `)} ${braces[1]}`;
15731573
}
15741574

1575+
function hasBuiltInToString(value) {
1576+
// Count objects that have no `toString` function as built-in.
1577+
if (typeof value.toString !== 'function') {
1578+
return true;
1579+
}
1580+
1581+
// The object has a own `toString` property. Thus it's not not a built-in one.
1582+
if (ObjectPrototypeHasOwnProperty(value, 'toString')) {
1583+
return false;
1584+
}
1585+
1586+
// Find the object that has the `toString` property as own property in the
1587+
// prototype chain.
1588+
let pointer = value;
1589+
do {
1590+
pointer = ObjectGetPrototypeOf(pointer);
1591+
} while (!ObjectPrototypeHasOwnProperty(pointer, 'toString'));
1592+
1593+
// Check closer if the object is a built-in.
1594+
const descriptor = ObjectGetOwnPropertyDescriptor(pointer, 'constructor');
1595+
return descriptor !== undefined &&
1596+
typeof descriptor.value === 'function' &&
1597+
builtInObjects.has(descriptor.value.name);
1598+
}
1599+
15751600
const firstErrorLine = (error) => error.message.split('\n')[0];
15761601
let CIRCULAR_ERROR_MESSAGE;
15771602
function tryStringify(arg) {
@@ -1630,29 +1655,17 @@ function formatWithOptionsInternal(inspectOptions, ...args) {
16301655
tempStr = formatNumber(stylizeNoColor, tempArg);
16311656
} else if (typeof tempArg === 'bigint') {
16321657
tempStr = `${tempArg}n`;
1658+
} else if (typeof tempArg !== 'object' ||
1659+
tempArg === null ||
1660+
!hasBuiltInToString(tempArg)) {
1661+
tempStr = String(tempArg);
16331662
} else {
1634-
let constr;
1635-
if (typeof tempArg !== 'object' ||
1636-
tempArg === null ||
1637-
(typeof tempArg.toString === 'function' &&
1638-
// A direct own property.
1639-
(ObjectPrototypeHasOwnProperty(tempArg, 'toString') ||
1640-
// A direct own property on the constructor prototype in
1641-
// case the constructor is not an built-in object.
1642-
((constr = tempArg.constructor) &&
1643-
!builtInObjects.has(constr.name) &&
1644-
constr.prototype &&
1645-
ObjectPrototypeHasOwnProperty(constr.prototype,
1646-
'toString'))))) {
1647-
tempStr = String(tempArg);
1648-
} else {
1649-
tempStr = inspect(tempArg, {
1650-
...inspectOptions,
1651-
compact: 3,
1652-
colors: false,
1653-
depth: 0
1654-
});
1655-
}
1663+
tempStr = inspect(tempArg, {
1664+
...inspectOptions,
1665+
compact: 3,
1666+
colors: false,
1667+
depth: 0
1668+
});
16561669
}
16571670
break;
16581671
case 106: // 'j'

test/parallel/test-util-format.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,66 @@ assert.strictEqual(util.format('%s', () => 5), '() => 5');
160160
util.format('%s', new Foobar(5)),
161161
'Foobar [ <5 empty items>, aaa: true ]'
162162
);
163+
164+
// Subclassing:
165+
class B extends Foo {}
166+
167+
function C() {}
168+
C.prototype.toString = function() {
169+
return 'Custom';
170+
};
171+
172+
function D() {
173+
C.call(this);
174+
}
175+
D.prototype = Object.create(C.prototype);
176+
177+
assert.strictEqual(
178+
util.format('%s', new B()),
179+
'Bar'
180+
);
181+
assert.strictEqual(
182+
util.format('%s', new C()),
183+
'Custom'
184+
);
185+
assert.strictEqual(
186+
util.format('%s', new D()),
187+
'Custom'
188+
);
189+
190+
D.prototype.constructor = D;
191+
assert.strictEqual(
192+
util.format('%s', new D()),
193+
'Custom'
194+
);
195+
196+
D.prototype.constructor = null;
197+
assert.strictEqual(
198+
util.format('%s', new D()),
199+
'Custom'
200+
);
201+
202+
D.prototype.constructor = { name: 'Foobar' };
203+
assert.strictEqual(
204+
util.format('%s', new D()),
205+
'Custom'
206+
);
207+
208+
Object.defineProperty(D.prototype, 'constructor', {
209+
get() {
210+
throw new Error();
211+
},
212+
configurable: true
213+
});
214+
assert.strictEqual(
215+
util.format('%s', new D()),
216+
'Custom'
217+
);
218+
219+
assert.strictEqual(
220+
util.format('%s', Object.create(null)),
221+
'[Object: null prototype] {}'
222+
);
163223
}
164224

165225
// JSON format specifier

0 commit comments

Comments
 (0)