The count returned by reading into an array is dropped, so partial reads go unnoticed

tx0/java:ignored-read-countdefaultwarningbug, cwe-252

What it finds

in.read(buffer); as a statement drops the number of bytes or characters read. A stream may fill only part of the array, at the end of the data or whenever less is available yet, and returns -1 at the end: code that goes on to use the whole buffer works on stale or missing data. readNBytes or DataInputStream.readFully read the whole amount, and a loop on the returned count handles partial reads.

Reported are read calls with an array as their first argument whose result is not used.

From the rule's source, ignored_read_count.rs.

Example from the rule's tests
import java.io.IOException;import java.io.InputStream; class Header {  byte[] read(InputStream in) throws IOException {    byte[] header = new byte[16];    in.read(header);tx0 reports: the number of elements read into header is dropped    int n = in.read(header, 0, 4);    in.read();    return header;  }}
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:ignored-read-count" = "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:ignored-read-count] reason="..."

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