Skip to content

specific scenario causes infinite recursion in util.inspect (max stack size exceeded) #37054

Closed
@timotejroiko

Description

@timotejroiko
  • Version: v15.6.0
  • Platform: Microsoft Windows NT 10.0.19042.0 x64
  • Subsystem: util

What steps will reproduce the bug?

const { inspect } = require("util");

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 test = new B();
const result = inspect(test, {
    depth:1,
    getters:true,
    showHidden:true
});

console.log(result);

How often does it reproduce? Is there a required condition?

Happens when both getters and showHidden are enabled and the class contains circular references accessible by getters

What is the expected behavior?

The depth option should short circuit the circular references and display no more than 2 levels of depth.

What do you see instead?

A giant wall of text.

Additional information

Also tested on node v15.4.0 and NWJS running node v15.3.0 but did not test in v14.

Issue discovered when attempting to inspect a client instance from the discord.js library, which makes use of such circular references.

EDIT: just tested in v15.2.0 and it works correctly. It seems the issue was introduced with v15.3.0

Metadata

Metadata

Assignees

Labels

confirmed-bugIssues with confirmed bugs.utilIssues and PRs related to the built-in util module.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions