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

Circular Reference Causes StackOverflowError in JsonWriter #128

Closed
testmigrator opened this issue Mar 6, 2025 · 1 comment
Closed

Circular Reference Causes StackOverflowError in JsonWriter #128

testmigrator opened this issue Mar 6, 2025 · 1 comment

Comments

@testmigrator
Copy link

testmigrator commented Mar 6, 2025

While writing a test case for circular reference serialization, I discovered that the current implementation of JsonWriter does not handle circular references properly, leading to a StackOverflowError.

@Test
public void testCircularReferenceSerialization() {
    // Create a dictionary and introduce a circular reference by setting one of its values to be the dictionary itself.
    JsonObject dict = new JsonObject(16, 0.75f);
    dict.put("a", dict);

    // Attempt to serialize the dictionary using a JSON serialization method.
    JsonWriter jsonWriter = new JsonWriter();
    try {
        jsonWriter.string(dict);
        fail("Expected a ValueError indicating the presence of a circular reference.");
    } catch (IllegalArgumentException e) {
        assertTrue(e.getMessage().contains("circular reference"), "The exception message should indicate a circular reference.");
    }
}

Expected Behavior:

The JsonWriter should detect the circular reference and throw an IllegalArgumentException with a message indicating the presence of a circular reference.

Actual Behavior:

The JsonWriter enters an infinite recursion due to the circular reference, resulting in a StackOverflowError.

Proposed Solution:

To handle circular references, the JsonWriter (or its base class, JsonWriterBase) should implement a mechanism to track visited objects during serialization. If a circular reference is detected, an exception should be thrown to prevent infinite recursion.

@testmigrator testmigrator changed the title Title: NullPointerException (NPE) due to circular reference in JsonWriter serialization Circular Reference Causes StackOverflowError in JsonWriter Mar 6, 2025
@mmastrac mmastrac closed this as not planned Won't fix, can't repro, duplicate, stale Mar 6, 2025
@testmigrator
Copy link
Author

This test is based on a real test from the Python simplejson library.

Thanks again for your time and effort on this project. Looking forward to your response!

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

No branches or pull requests

2 participants