Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
timotejroiko opened this issue Jan 24, 2021 · 4 comments
Assignees
Labels
confirmed-bug Issues with confirmed bugs. util Issues and PRs related to the built-in util module.

Comments

@timotejroiko
Copy link

timotejroiko commented Jan 24, 2021

  • 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

@targos targos added the util Issues and PRs related to the built-in util module. label Jan 25, 2021
@RaisinTen
Copy link
Contributor

This runs correctly in v15.2.0:

❯ node test.js
<ref *1> B {
  A: A { B: [Circular *1], [b]: [Getter: undefined] },
  [a]: [Getter: undefined]
}

@timotejroiko
Copy link
Author

This runs correctly in v15.2.0:

Can confirm, just tested myself on 15.2 and it worked correctly. Issue happens on 15.3+

@targos
Copy link
Member

targos commented Jan 25, 2021

Probably introduced by #36052

@BridgeAR BridgeAR self-assigned this Jan 26, 2021
@BridgeAR BridgeAR added the confirmed-bug Issues with confirmed bugs. label Jan 26, 2021
@BridgeAR
Copy link
Member

BridgeAR commented Jan 26, 2021

I got a fix locally and I'll submit the PR later on.

> console.log(result);
<ref *1> B {
  A: A { B: [Circular *1], [b]: [Getter] [Circular *1] },
  [a]: [Getter] A { B: [Circular *1], [b]: [Getter] [Circular *1] }
}

jasnell pushed a commit that referenced this issue Apr 30, 2021
The documentation and layout example was not up to date with the
latest changes.

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

PR-URL: #37079
Fixes: #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>
targos pushed a commit that referenced this issue May 3, 2021
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: #37079
Fixes: #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>
targos pushed a commit that referenced this issue May 3, 2021
The documentation and layout example was not up to date with the
latest changes.

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

PR-URL: #37079
Fixes: #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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug Issues with confirmed bugs. util Issues and PRs related to the built-in util module.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants