read() of an input stream returns a signed byte, so bytes above 127 look like the end

tx0/java:signed-byte-readdefaultwarningbug

What it finds

InputStream.read() returns a byte as a number from 0 to 255, and -1 at the end of the stream. An override that returns a byte directly, as return buffer[position++];, returns -1 to -128 for bytes above 127: callers take the first such byte for the end of the stream. buffer[position++] & 0xFF gives the value the contract asks for.

Reported in read() methods of classes that extend InputStream or FilterInputStream, for returned values of type byte.

From the rule's source, signed_byte_read.rs.

Example from the rule's tests
import java.io.InputStream; class BufferStream extends InputStream {  private final byte[] buffer = new byte[8];  private int position;   @Override  public int read() {    if (position == buffer.length) {      return -1;    }    return buffer[position++];tx0 reports: this returns a signed byte  }} class GoodStream extends InputStream {  private final byte[] buffer = new byte[8];  private int position;   @Override  public int read() {    return buffer[position++] & 0xFF;  }}
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:signed-byte-read" = "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:signed-byte-read] reason="..."

tx0 explain tx0/java:signed-byte-read shows the same in the terminal. Found a false positive? Report it.