OpenHFT / Chronicle-Wire

A Low Garbage Java Serialisation Library that supports multiple formats
http://chronicle.software
Apache License 2.0
525 stars 124 forks source link

Deserialization of StringBuilder[] Produces Array with Null Elements #949

Closed Khush-dev closed 1 month ago

Khush-dev commented 1 month ago

Deserialising StringBuilder[] produces array with null elements.

Steps to Reproduce

package org.example;

import net.openhft.chronicle.bytes.Bytes;
import net.openhft.chronicle.wire.BinaryWire;
import net.openhft.chronicle.wire.SelfDescribingMarshallable;

public class Main {

    public static void main(String[] args) {
        var bytes = Bytes.allocateElasticOnHeap();
        var wire = new BinaryWire(bytes);
        var foo1 = new Foo();
        foo1.bar = new StringBuilder[] { new StringBuilder("baz") };
        wire.write("data").object(foo1);
        var foo2 = wire.read("data").object(Foo.class);
        System.out.println(foo2.bar[0]);
    }

    public static class Foo extends SelfDescribingMarshallable {
        StringBuilder[] bar;
    }

}

Expected Output

baz

Current Output

null

Likely Bug Location

https://github.com/OpenHFT/Chronicle-Wire/blob/ea/src/main/java/net/openhft/chronicle/wire/Wires.java#L1504-L1513

                case "java.lang.StringBuilder":
                    return ScalarStrategy.of(StringBuilder.class, (o, in) -> {
                        try (ScopedResource<StringBuilder> stlSb = Wires.acquireStringBuilderScoped()) {
                            StringBuilder builder = (o == null)
                                    ? stlSb.get()
                                    : o;
                            in.textTo(builder);
                        }
                        return o;
                    });

Here, when o is null, builder from stlSb.get() is updated and closed instead of creating a new StringBuilder object.

tgd commented 1 month ago

Thanks for the bug report - we will take a look

james-mcsherry commented 1 month ago

Hi @Khush-dev - this issue has been fixed. Thank you for reporting, once the latest version is released I will post back here.