Deserialization of a linked list of records

Remi Forax forax at univ-mlv.fr
Fri Feb 26 22:06:49 UTC 2021


[sent to both email list given it's not clear if it's an implementation issue or a spec issue]

There is a nice video of how the serialization of records works recently on inside.java
https://inside.java/2021/02/23/records-met-serialization/

In the video, Julia explains that the de-serialization works from bottom-up, so what if the record instances are a linked list ...
answer: a stack overflow.

Here is a small reproducer.

import static java.util.stream.IntStream.range;

public class RecordSerializationFailure {
  record Link(Object value, Link next) implements Serializable {}

  public static void main(String[] args) throws IOException, ClassNotFoundException {
    var link = range(0, 1_000_000)
        .boxed()
        .<Link>reduce(null, (next, i) -> new Link(i, next), (_1, _2) -> null);

    var path = Path.of("serial.data");
    try(var output = Files.newOutputStream(path);
        var oos = new ObjectOutputStream(output)) {
      oos.writeObject(link);
    }

    Link result;
    try(var input = Files.newInputStream(path);
        var ois = new ObjectInputStream(input)) {
      result = (Link) ois.readObject();
    }

    System.out.println(link.equals(result));
  }
}

Should this be fixed or not ?

regards,
Rémi


More information about the amber-dev mailing list