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

JSON.stringify should throw TypeError("cyclic object value") when a circular reference is found. #545

Closed
n14little opened this issue Jul 3, 2020 · 3 comments · Fixed by #777
Assignees
Labels
bug Something isn't working builtins PRs and Issues related to builtins/intrinsics good first issue Good for newcomers

Comments

@n14little
Copy link
Contributor

Describe the bug
JSON.stringify should throw a TypeError when it encounters a circular reference. Currently, the stack overflows.

To Reproduce

let a = [];
a[0] = a;
JSON.stringify(a);

Expected behavior
A TypeError should be thrown. See NOTE 1 in the spec

Build environment (please complete the following information):

  • OS: Ubuntu Linux
  • Version: 20.04
  • Target triple: x86_64-unknown-linux-gnu
  • Rustc version: rustc 1.43.1 (8d69840ab 2020-05-04)

Additional context
You can find more information on MDN.

@n14little n14little added the bug Something isn't working label Jul 3, 2020
@Razican Razican added builtins PRs and Issues related to builtins/intrinsics good first issue Good for newcomers labels Jul 8, 2020
@Razican
Copy link
Member

Razican commented Jul 8, 2020

To solve this, we need to modify the JSON.stringify() function, implemented here.

Here, before adding a new property to the object to return, we need to check if the object to return already has the same value as a parent. For cases where we have a replacer, this is done directly in that function. If we don't, then we need to go to the to_json() function in Value.

Here, we would need to check before inserting a new field to the JSON object that the same object is not already a parent of the current object.

To do that, we should use Gc::ptr_eq() with all parent values, so we need to store them just in case. If we find that they are the same, then we just throw a type error.

@vgel
Copy link
Contributor

vgel commented Oct 2, 2020

I can work on this.

@RageKnify
Copy link
Member

Not the same bug, but related, a.toString() also results in a stack overflow. I'm trying to understand what the spec says about a.toString() in this situation, but both Firefox and Chrome return an empty string, "".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working builtins PRs and Issues related to builtins/intrinsics good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants