An ObjectOutputStream appends to a file, adding a second stream header that readers reject

tx0/java:append-object-streamdefaulterrorbug

What it finds

An ObjectOutputStream writes a header when it is created, and an ObjectInputStream expects exactly one header at the start. Opening a file in append mode and wrapping it in a new ObjectOutputStream puts a second header in the middle of the file, and reading back fails there with a StreamCorruptedException, as the java.io serialization documentation implies.

Reported is an ObjectOutputStream created on new FileOutputStream(file, true) or on Files.newOutputStream with StandardOpenOption.APPEND, directly or through a local variable initialized with one.

From the rule's source, append_object_stream.rs.

Example from the rule's tests
import java.io.FileOutputStream;import java.io.IOException;import java.io.ObjectOutputStream; class Journal {  void write(String path, Object entry) throws IOException {    ObjectOutputStream a = new ObjectOutputStream(new FileOutputStream(path, true));tx0 reports: this stream appends to a file    FileOutputStream file = new FileOutputStream(path, true);    ObjectOutputStream b = new ObjectOutputStream(file);tx0 reports: this stream appends to a file    ObjectOutputStream c = new ObjectOutputStream(new FileOutputStream(path));  }}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs in every tier. To turn it off for a project, set its level in tx0.toml:

tx0.toml
[rules]
"tx0/java:append-object-stream" = "off"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/java:append-object-stream] reason="..."

tx0 explain tx0/java:append-object-stream shows the same in the terminal. Found a false positive? Report it.