A signed byte is combined with shifted bits without a mask, so its sign bits overwrite them

tx0/java:sign-extended-byterecommendedwarningbug

What it finds

(bytes[0] << 8) | bytes[1] meant to join two bytes into one number, but a byte is signed: promoted to int, a byte of 0x80 or more becomes negative, with all high bits set, and the | overwrites the bits shifted in from the other byte. Each byte needs & 0xFF before it is combined, as the promotion rules of the language specification imply.

Reported is a byte variable or array element, without a mask, as an operand of | whose other operand is or contains a left shift.

From the rule's source, sign_extended_byte.rs.

Example from the rule's tests
class Decoder {  int read(byte[] data, byte low) {    int a = (data[0] << 8) | data[1];tx0 reports: data[1] is a signed byte    int b = ((data[0] & 0xFF) << 8) | (data[1] & 0xFF);    int c = (data[2] << 8) | low;tx0 reports: low is a signed byte    int d = data[0] | data[1];    return a + b + c + d;  }}
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 from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/java:sign-extended-byte" = "warning"

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:sign-extended-byte] reason="..."

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