Skip to content

CachedFields are not reset when references-flag changes #973

@thomas-z96

Description

@thomas-z96

Describe the bug
When changing the references flag from false to true on a kryo instance that has already been used for deserializing, subsequent deserialization does not work correctly any more (wrong results / exceptions).

To Reproduce

@Test
public void testBug() {
    Output output1 = new Output(512, -1);
    Kryo kryo = getKryoInstance(false);
    kryo.writeObject(output1, new TestString("abc"));

    Output output2 = new Output(512, -1);
    kryo = getKryoInstance(true);
    kryo.writeObject(output2, new TestString("abc"));

    Input input1 = new Input(output1.getBuffer());
    kryo = getKryoInstance(false);
    assertEquals("abc", kryo.readObject(input1, TestString.class).str);

    Input input2 = new Input(output2.getBuffer());
    kryo.setReferences(true);
    assertEquals("abc", kryo.readObject(input2, TestString.class).str);
}

static class TestString {
    String str;
    TestString() {
    }
    TestString(String str) {
        this.str = str;
    }
    @Override
    public String toString() {
        return "TestString [str=" + str + "]";
    }
}

private Kryo getKryoInstance(boolean references) {
    Kryo kryo = new Kryo();
    kryo.setRegistrationRequired(false);
    kryo.setReferences(references);
    return kryo;
}

Environment:

  • OS: Windows 10
  • JDK Version: 11
  • Kryo Version: 5.5.0

Additional context
The issue is that CachedFields.newUnsafeField() / CachedFields.newAsmField() check the references flag of the kryo instance and return different CachedField instances depending on that flag. This CachedField is then kept in the FieldSerializer's cachedFields and is not removed if the flag changes, so the wrong CachedField type is used for subsequent deserializations.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions