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

Improve cycle checks in JSON.stringify #700

Merged

Conversation

saulecabrera
Copy link
Member

@saulecabrera saulecabrera commented Jul 11, 2024

Prior to this commit, the following program will erroneously fail, with a circular dependency error:

const template = {
  foo: "foo",
  bar: "bar",
};
const x = [];

x.push(template);
x.push(template);
x.push(template);
x.push(template);
x.push(template);
x.push(template);

console.log(JSON.stringify(x));

The logic for testing for circular dependencies didn't correctly manage the stack state and therefore pushing the same object multiple times to an array resulted in a false-positive cycle.

Cycles can only really happen in Arrays and Objects. This commit solidifies the approach to checking for cycles, by scoping cycle checks and cycle stack management to the MapAccess and SeqAccess structs.

Checklist

  • I've updated the relevant CHANGELOG files if necessary. Changes to javy-cli and javy-core do not require updating CHANGELOG files.
  • I've updated the relevant crate versions if necessary. Versioning policy for library crates
  • I've updated documentation including crate documentation if necessary.

Prior to this commit, the following program will erroneously fail, with
a `circular dependency` error:

    const template = {
      foo: "foo",
      bar: "bar",
    };
    const x = [];

    x.push(template);
    x.push(template);
    x.push(template);
    x.push(template);
    x.push(template);
    x.push(template);

    console.log(JSON.stringify(x));

The logic for testing for circular dependencies didn't correctly manage
the stack state and therefore pushing the same object multiple times to
an array resulted in a false-positive cycle.

Cycles can only really happen in Arrays and Objects. Thsi commit
solidifies the approach to checking for cycles, by scoping cycle checks
and cycle stack management to the `MapAccess` and `SeqAccess` structs.
crates/javy/src/serde/de.rs Outdated Show resolved Hide resolved
crates/javy/src/serde/de.rs Outdated Show resolved Hide resolved
@saulecabrera saulecabrera enabled auto-merge (squash) July 11, 2024 17:57
@saulecabrera saulecabrera merged commit 6f057ea into bytecodealliance:main Jul 11, 2024
16 checks passed
@saulecabrera saulecabrera deleted the fix-recursive-stringify branch July 11, 2024 22:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants