node/test/parallel/test-util-inspect-getters-accessing-this.js
Ruben Bridgewater 942b76d9e5
util: fix infinite recursion during inspection
A specially crafted object with circular structures behind getters
could cause a infinite recursion. This is now fixed by detecting the
objects as already visited.

Signed-off-by: Ruben Bridgewater <ruben@bridgewater.de>

PR-URL: https://github.com/nodejs/node/pull/37079
Fixes: https://github.com/nodejs/node/issues/37054
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Anto Aravinth <anto.aravinth.cse@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
2021-04-30 12:46:48 -07:00

68 lines
1.0 KiB
JavaScript

'use strict';
require('../common');
// This test ensures that util.inspect logs getters
// which access this.
const assert = require('assert');
const { inspect } = require('util');
{
class X {
constructor() {
this._y = 123;
}
get y() {
return this._y;
}
}
const result = inspect(new X(), {
getters: true,
showHidden: true
});
assert.strictEqual(
result,
'X { _y: 123, [y]: [Getter: 123] }'
);
}
// Regression test for https://github.com/nodejs/node/issues/37054
{
class A {
constructor(B) {
this.B = B;
}
get b() {
return this.B;
}
}
class B {
constructor() {
this.A = new A(this);
}
get a() {
return this.A;
}
}
const result = inspect(new B(), {
depth: 1,
getters: true,
showHidden: true
});
assert.strictEqual(
result,
'<ref *1> B {\n' +
' A: A { B: [Circular *1], [b]: [Getter] [Circular *1] },\n' +
' [a]: [Getter] A { B: [Circular *1], [b]: [Getter] [Circular *1] }\n' +
'}',
);
}